FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?

Discussion in 'Javascript' started by FAQ server, Oct 13, 2007.

  1. FAQ server

    FAQ server Guest

    -----------------------------------------------------------------------
    FAQ Topic - How do I convert a Number into a String with
    exactly 2 decimal places?
    -----------------------------------------------------------------------

    When formatting money for example, to format 6.57634 to
    6.58, 6.5 to 6.50, and 6 to 6.00?

    Rounding of x.xx5 is uncertain, as such numbers are not
    represented exactly. See section 4.7 for Rounding issues.

    N = Math.round(N*100)/100 only converts N to a Number of value
    close to a multiple of 0.01; but document.write(N) does not give
    trailing zeroes.

    ECMAScript Ed. 3.0 (JScript 5.5 [but buggy] and JavaScript 1.5)
    introduced N.toFixed, the main problem with this is the bugs in
    JScripts implementation.

    Most implementations fail with certain numbers, for example 0.07.
    The following works successfully for M>0, N>0:

    function Stretch(Q, L, c) { var S = Q
    if (c.length>0) while (S.length<L) { S = c+S }
    return S
    }
    function StrU(X, M, N) { // X>=0.0
    var T, S=new String(Math.round(X*Number("1e"+N)))
    if (S.search && S.search(/\D/)!=-1) { return ''+X }
    with (new String(Stretch(S, M+N, '0')))
    return substring(0, T=(length-N)) + '.' + substring(T)
    }
    function Sign(X) { return X>0 ? "+" : X<0 ? "-" : " " }
    function StrS(X, M, N) { return Sign(X)+StrU(Math.abs(X), M, N) }
    Number.prototype.toFixed= function(n){ return StrS(this,1,n)};

    http://www.merlyn.demon.co.uk/js-round.htm

    http://msdn.microsoft.com/library/d...html/b5f03400-865e-4ab2-818c-f734c0f6d6f0.asp


    --
    Postings such as this are automatically sent once a day. Their
    goal is to answer repeated questions, and to offer the content to
    the community for continuous evaluation/improvement. The complete
    comp.lang.javascript FAQ is at http://jibbering.com/faq/index.html.
    The FAQ workers are a group of volunteers. The sendings of these
    daily posts are proficiently hosted by http://www.pair.com.
     
    FAQ server, Oct 13, 2007
    #1
    1. Advertising

  2. FAQ server

    Evertjan. Guest

    FAQ server wrote on 13 okt 2007 in comp.lang.javascript:

    > Rounding of x.xx5 is uncertain, as such numbers are not
    > represented exactly.


    I do not see that:

    n = 212345.50

    // [say yo define .5 to be rounded up]

    alert(n)


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Oct 13, 2007
    #2
    1. Advertising

  3. FAQ server

    Evertjan. Guest

    Evertjan. wrote on 13 okt 2007 in comp.lang.javascript:

    > FAQ server wrote on 13 okt 2007 in comp.lang.javascript:
    >
    >> Rounding of x.xx5 is uncertain, as such numbers are not
    >> represented exactly.

    >
    > I do not see that:
    >
    > n = 212345.50
    >
    > // [say yo define .5 to be rounded up]
    >
    > alert(n)



    Sorry, please disregard, I pressed the "send" too fast.

    I will come back on this, have to go now.


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Oct 13, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. FAQ server
    Replies:
    6
    Views:
    173
    Dr J R Stockton
    Nov 3, 2006
  2. FAQ server
    Replies:
    52
    Views:
    540
  3. FAQ server
    Replies:
    28
    Views:
    313
    Randy Webb
    Feb 23, 2007
  4. FAQ server
    Replies:
    1
    Views:
    155
    Dr J R Stockton
    Apr 15, 2007
  5. FAQ server
    Replies:
    0
    Views:
    108
    FAQ server
    Jun 14, 2007
Loading...

Share This Page