Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #18915

Re: FAQ Topic - How do I format a Number as a String with exactly 2 decimal places? (2013-03-15)

From Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid>
Newsgroups comp.lang.javascript
Subject Re: FAQ Topic - How do I format a Number as a String with exactly 2 decimal places? (2013-03-15)
Date 2013-03-16 19:57 +0000
Organization Home
Message-ID <aZewtRTO6MRRFw0Z@invalid.uk.co.demon.merlyn.invalid> (permalink)
References <51426487$0$32115$14726298@news.sunsite.dk> <XnsA18460B31AE76eejj99@194.109.133.133>

Show all headers | View raw


In comp.lang.javascript message <XnsA18460B31AE76eejj99@194.109.133.133>
, Fri, 15 Mar 2013 09:30:21, Evertjan. <exxjxw.hannivoort@inter.nl.net>
posted:

>FAQ server wrote on 15 mrt 2013 in comp.lang.javascript:
>
>> -----------------------------------------------------------------------
>> FAQ Topic - How do I format a Number as a String with
>> exactly 2 decimal places?
>> -----------------------------------------------------------------------
>>
>> When formatting money for example, to format 6.57634 to 6.58, 6.7 to
>> 6.50, and 6 to 6.00?
>
>[Negative values should be included in this specification]
>
>function formatNumber2(n) {
>   sign = (n<0) ?'-' :'';
>   n = Math.abs(n * 100 + .5);
>   n = Math.floor(n).toString();
>   while (n.length<3) n = '0' + n;
>   n = n.replace(/(\d{2})$/,'.$1');
>   return sign + n;
>};
>
>//====== test: =======
>
>var n = -.0657634;
>document.write(formatNumber2(n) + '<br>');
>
>var n = .999999;
>document.write(formatNumber2(n) + '<br>');
>
>var n = -12.01999;
>document.write(formatNumber2(n) + '<br>');
>
>var n = -0;
>document.write(formatNumber2(n) + '<br>');

I expect that you did not test it with n = -33, or n = null.  The one in
the FAQ is better; it does -33 correctly.  I used current Windows XP
Firefox.

Try StrS, in <http://www.merlyn.demon.co.uk/js-rndg1.htm#GC>.

-- 
 (c) John Stockton, nr London UK. Mail, see Homepage. BP7, Delphi 3 & 2006.
   <http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
   <http://www.bancoems.com/CompLangPascalDelphiMisc-MiniFAQ.htm> clpdmFAQ;
   NOT <http://support.codegear.com/newsgroups/>: news:borland.* Guidelines

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

FAQ Topic - How do I format a Number as a String with exactly 2 decimal places? (2013-03-15) "FAQ server" <javascript@dotinternet.be> - 2013-03-15 00:00 +0000
  Re: FAQ Topic - How do I format a Number as a String with exactly 2 decimal places? (2013-03-15) "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-03-15 09:30 +0100
    Re: FAQ Topic - How do I format a Number as a String with exactly 2 decimal places? (2013-03-15) Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-03-16 19:57 +0000
      Re: FAQ Topic - How do I format a Number as a String with exactly 2 decimal places? (2013-03-15) "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-03-17 19:36 +0100
        Re: FAQ Topic - How do I format a Number as a String with exactly 2 decimal places? (2013-03-15) Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-03-18 19:21 +0000
          Re: FAQ Topic - How do I format a Number as a String with exactly 2 decimal places? (2013-03-15) "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-03-19 08:43 +0100

csiph-web