Currency Conversion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • News Guy

    Currency Conversion

    Hello,

    Can someone tell me an easy way to convert a number with many trailing
    digits to a currency format and adding the '$' sign? Thanks.

    example

    Convert 34.77389993 to $34.77

    Thanks, News Guy


  • Michael Winter

    #2
    Re: Currency Conversion

    On 03/10/2005 18:55, News Guy wrote:
    [color=blue]
    > Can someone tell me an easy way to convert a number with many trailing
    > digits to a currency format and adding the '$' sign? Thanks.[/color]

    /* n - Number to format (in pennies).
    * c - Currency symbol to use (defaults to none).
    * g - Thousands symbol (defaults to none).
    * d - Decimal separator (defaults to dot [.]).
    *
    * Outputs a number of the form cngnnngnnn.nn
    *
    * For example, toCurrency(1426 35.7, '£', ',') produces
    * £1,426.36
    */
    function toCurrency(n, c, g, d) {
    var s = (0 > n) ? '-' : '';
    var m = String(Math.rou nd(Math.abs(n)) );
    var i = '', j, f; c = c || ''; g = g || ''; d = d || '.';

    while(m.length < 3) {m = '0' + m;}
    f = m.substring((j = m.length - 2));
    while(j > 3) {
    i = g + m.substring(j - 3, j) + i;
    j -= 3;
    }
    i = m.substring(0, j) + i;
    return s + c + i + d + f;
    }

    Take note of the arguments.

    Mike

    --
    Michael Winter
    Prefix subject with [News] before replying by e-mail.

    Comment

    • Danny

      #3
      Re: Currency Conversion



      Simple:

      var aNum=34.7738999 3; formattedNum='$ '+aNum.toFixed( 2);

      the '$' has to be string, then just use .toFixed() to limit the floating.


      Danny

      Comment

      • Randy Webb

        #4
        Re: Currency Conversion

        Danny said the following on 10/2/2005 2:08 AM:
        [color=blue]
        >
        > Simple:
        >
        > var aNum=34.7738999 3; formattedNum='$ '+aNum.toFixed( 2);
        >
        > the '$' has to be string, then just use .toFixed() to limit the floating.[/color]

        And then worry about the bugs in .toFixed()

        --
        Randy
        comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly

        Comment

        • Dr John Stockton

          #5
          Re: Currency Conversion

          JRS: In article <sge0f.535$647. 520@news01.roc. ny>, dated Mon, 3 Oct
          2005 17:55:36, seen in news:comp.lang. javascript, News Guy
          <news@yahoo.com > posted :
          [color=blue]
          >Can someone tell me an easy way to convert a number with many trailing
          >digits to a currency format and adding the '$' sign? Thanks.[/color]

          Please read the newsgroup FAQ before asking about what is manifestly a
          common need.

          Numbers are binary, and don't have trailing digits; however, the default
          conversion to String may give various non-zero numbers of digits after
          the decimal point.

          See <URL:http://www.merlyn.demo n.co.uk/js-round.htm>

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
          <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
          <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

          Comment

          • News Guy

            #6
            Re: Currency Conversion

            Thanks Danny, works great!

            News Guy



            "Danny" <dann90038@blue bottle.com> wrote in message
            news:1128398633 .7c35f260384862 cbc5fe9f4bf15e1 b8b@teranews...[color=blue]
            >
            >
            > Simple:
            >
            > var aNum=34.7738999 3; formattedNum='$ '+aNum.toFixed( 2);
            >
            > the '$' has to be string, then just use .toFixed() to limit the
            > floating.
            >
            >
            > Danny[/color]


            Comment

            • Evertjan.

              #7
              Re: Currency Conversion

              News Guy wrote on 04 okt 2005 in comp.lang.javas cript:[color=blue]
              > "Danny" <dann90038@blue bottle.com> wrote in message[color=green]
              >> Simple:
              >> var aNum=34.7738999 3; formattedNum='$ '+aNum.toFixed( 2);[/color][/color]
              [color=blue]
              > Thanks Danny, works great![/color]

              Testing in IE6:

              var aNum=0.0094; formattedNum='$ '+aNum.toFixed( 2);

              // gives 0.00

              var aNum=34.0094; formattedNum='$ '+aNum.toFixed( 2);

              // gives 34.01

              Not so great, eh?

              --
              Evertjan.
              The Netherlands.
              (Replace all crosses with dots in my emailaddress)

              Comment

              Working...