Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #18922
| 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) |
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
| References | <51426487$0$32115$14726298@news.sunsite.dk> <XnsA18460B31AE76eejj99@194.109.133.133> <aZewtRTO6MRRFw0Z@invalid.uk.co.demon.merlyn.invalid> |
| Date | 2013-03-17 19:36 +0100 |
| Message-ID | <XnsA186C77818CE7eejj99@194.109.133.133> (permalink) |
Dr J R Stockton wrote on 16 mrt 2013 in comp.lang.javascript:
> 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);
The above line should be:
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.
No reason why it should work with null.
Should it work for n = 'blah';,
or for n being an object?
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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