FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places? (2008-10-29)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • FAQ server

    #1

    FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places? (2008-10-29)

    -----------------------------------------------------------------------
    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 the section on "simple decimal arithmetic"
    for Rounding issues.

    ` N = Math.round(N*10 0)/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 ` Number.prototyp e.toFixed `, the main problem
    with this is the bugs in JScript's 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 = String(Math.rou nd(X * Number("1e" + N)));
    if (S.search && S.search(/\D/) != -1) {
    return '' + X;
    }
    with (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.prototyp e.toFixed = function(n){
    return StrS(this, 1, n);
    };



    http://msdn2.microsoft.com/en-us/library/sstyff0z.aspx


    --
    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.javas cript 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.

  • Dr J R Stockton

    #2
    Re: FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places? (2008-10-29)

    In comp.lang.javas cript message <4907a788$0$902 69$14726298@new s.sunsite.
    dk>, Wed, 29 Oct 2008 00:00:02, FAQ server <javascript@dot internet.be>
    posted:

    And did so in a very timely manner, the topic having been raised in the
    "JavaScript Math vs Excel" thread.
    >FAQ Topic - How do I convert a Number into a String with
    >exactly 2 decimal places?
    ...
    To that line should be appended an indication that the logically-
    following pages should also be read; my current StrU is in the next.
    I'd just append either " ff." or " /ff./", expecting readers to
    understand it. YMMV.

    The functions are the FAQ are NOT what I presently use. My present
    ones, StrU ff. at <URL:http://www.merlyn.demo n.co.uk/js-rndg1.htm#GC>
    give the called-for not-too-big numeric fixed-point decimal result when
    possible, and otherwise indicate exactly what the input value was,
    including both Infinities, NaN, null, undefined - and with the correct
    number of spaces, if necessary (the string may be too long, but not too
    short). I was about to assert that they thereby cover all
    possibilities, when I realised that there was one omission - and that I
    could easily fix it.

    I'll let you all think about it for a bit ...

    --
    (c) John Stockton, near London. *@merlyn.demon. co.uk/?.?.Stockton@ph ysics.org
    Web <URL:http://www.merlyn.demo n.co.uk/- FAQish topics, acronyms, & links.
    Correct <= 4-line sig. separator as above, a line precisely "-- " (SoRFC1036)
    Do not Mail News to me. Before a reply, quote with ">" or "" (SoRFC1036)

    Comment

    • Dr J R Stockton

      #3
      Re: FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places? (2008-10-29)

      In comp.lang.javas cript message <QEUjdwRYPMCJFw sm@invalid.uk.c o.demon.me
      rlyn.invalid>, Wed, 29 Oct 2008 20:13:12, Dr J R Stockton
      <jrs@merlyn.dem on.co.ukposted:
      >>FAQ Topic - How do I convert a Number into a String with
      >>exactly 2 decimal places?
      >The functions are the FAQ are NOT what I presently use. My present
      >ones, StrU ff. at <URL:http://www.merlyn.demo n.co.uk/js-rndg1.htm#GC>
      ...
      I was about to assert that they thereby cover all
      >possibilitie s, when I realised that there was one omission - and that I
      >could easily fix it.
      >
      >I'll let you all think about it for a bit ...
      Over two days have elapsed. Neither the routines in the FAQ, nor any
      others that I had seen (my own included) up to Wednesday evening, can
      represent the difference between +0 and -0.

      The present FAQ routines give a leading space for +0 and -0, and an
      appropriate sign for all other numbers, including infinities and those
      which round to zero.

      To get a signed zero, replace function Sign with
      function Sygn(X) { return X + 1/X 0 ? "+" : "-" }

      So, in the FAQ, keep Sign (being more commonly wanted?), but give Sygn
      as well.

      The FAQ routines survive missing arguments. They also survive the first
      argument being a Function, Array, or Object. I have been wondering
      whether what they then do can be improved without adding too much code.

      The FAQ is now visible at the URL in FAQ 9.91 :-) .

      --
      (c) John Stockton, nr London UK. ?@merlyn.demon. co.uk BP7, Delphi 3 & 2006.
      <URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/&c., FAQqy topics & links;
      <URL:http://www.bancoems.co m/CompLangPascalD elphiMisc-MiniFAQ.htmclpd mFAQ;
      NOT <URL:http://support.codegea r.com/newsgroups/>: news:borland.* Guidelines

      Comment

      Working...