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

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

FAQ server
Guest
Posts: n/a

 12-13-2006
-----------------------------------------------------------------------
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.

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 ? '-' : ''; }
function StrS(X, M, N) { return Sign(X)+StrU(Math.abs(X), M, N) }

Number.prototype.toFixed= new Function('n','return StrS(this,1,n)')

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

http://msdn.microsoft.com/library/en...mthToFixed.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://www.jibbering.com/faq/.
The FAQ workers are a group of volunteers.

Dr J R Stockton
Guest
Posts: n/a

 12-13-2006
In comp.lang.javascript message
<457f428a\$0\$49209\$(E-Mail Removed)>, Wed, 13 Dec 2006 00:00:01,
FAQ server <(E-Mail Removed)> wrote:

> 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 ? '-' : ''; }
> function StrS(X, M, N) { return Sign(X)+StrU(Math.abs(X), M, N) }

The functions on my site have been updated since that was put in the
FAQ.

function Sign(X) { return X>0 ? "+" : X<0 ? "-" : " " }

function PrfxTo(S, L, C) { S += ""
if (C.length>0) while (S.length<L) S = C + S ; return S }

function SpcsTo(S, L) { S += "" // SpcsTo is a reduction of PrfxTo
while (S.length<L) S = " " + S ; return S }

function StrU(X, M, N) { // X > -0.5e-N ; to M digits point N digits
var S = String(Math.round(X*Math.pow(10, N)))
if (/\D/.test(S)) return SpcsTo(X, M+N+1) // cannot cope
S = PrfxTo(S, M+N, '0') ; var T = S.length - N
return S.substring(0, T) + '.' + S.substring(T) }

function StrS(X, M, N) { return Sign(X) + StrU(Math.abs(X), M, N) }

Given that some of the function set are intended to give fixed-width
results, ISTM that Sign should give fixed-width output. When that's
*not* wanted, removing a bit of the code will be easy.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.com/faq/> A FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.

Randy Webb
Guest
Posts: n/a

 12-14-2006
Dr J R Stockton said the following on 12/13/2006 5:32 PM:
> In comp.lang.javascript message
> <457f428a\$0\$49209\$(E-Mail Removed)>, Wed, 13 Dec 2006 00:00:01,
> FAQ server <(E-Mail Removed)> wrote:
>
>> 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 ? '-' : ''; }
>> function StrS(X, M, N) { return Sign(X)+StrU(Math.abs(X), M, N) }

>
> The functions on my site have been updated since that was put in the FAQ.
>
>
>
> function Sign(X) { return X>0 ? "+" : X<0 ? "-" : " " }
>
> function PrfxTo(S, L, C) { S += ""
> if (C.length>0) while (S.length<L) S = C + S ; return S }
>
> function SpcsTo(S, L) { S += "" // SpcsTo is a reduction of PrfxTo
> while (S.length<L) S = " " + S ; return S }
>
> function StrU(X, M, N) { // X > -0.5e-N ; to M digits point N digits
> var S = String(Math.round(X*Math.pow(10, N)))
> if (/\D/.test(S)) return SpcsTo(X, M+N+1) // cannot cope
> S = PrfxTo(S, M+N, '0') ; var T = S.length - N
> return S.substring(0, T) + '.' + S.substring(T) }
>
> function StrS(X, M, N) { return Sign(X) + StrU(Math.abs(X), M, N) }
>
>
> Given that some of the function set are intended to give fixed-width
> results, ISTM that Sign should give fixed-width output. When that's
> *not* wanted, removing a bit of the code will be easy.

All of the code snippets, every one of them, will be reviewed and
re-written for 10.0 with some documentation and better variable names to
make them easier for newbes to be able to read and learn from. The FAQ
isn't geared to experienced users that can decipher code such as that
above, it is for people who *can't* write code such as that above but
want to learn from it.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

RobG
Guest
Posts: n/a

 12-14-2006

Randy Webb wrote:
[...]
> All of the code snippets, every one of them, will be reviewed and
> re-written for 10.0 with some documentation and better variable names to
> make them easier for newbes to be able to read and learn from. The FAQ

[...]
> is for people who *can't* write code such as that above but
> want to learn from it.

Amen to that.

--
Rob

Dr J R Stockton
Guest
Posts: n/a

 12-14-2006
In comp.lang.javascript message <(E-Mail Removed)>,
Wed, 13 Dec 2006 21:35:57, Randy Webb <(E-Mail Removed)> wrote:
>
>All of the code snippets, every one of them, will be reviewed and re-
>written for 10.0 with some documentation and better variable names to
>make them easier for newbes to be able to read and learn from. The FAQ
>isn't geared to experienced users that can decipher code such as that
>above, it is for people who *can't* write code such as that above but
>want to learn from it.

Where a FAQ entry is headed by a request for an algorithm, a coding
tutorial is not called for. The Notes are the place for that.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

Randy Webb
Guest
Posts: n/a

 12-15-2006
Dr J R Stockton said the following on 12/14/2006 4:09 PM:
> In comp.lang.javascript message <(E-Mail Removed)>,
> Wed, 13 Dec 2006 21:35:57, Randy Webb <(E-Mail Removed)> wrote:
>> All of the code snippets, every one of them, will be reviewed and re-
>> written for 10.0 with some documentation and better variable names to
>> make them easier for newbes to be able to read and learn from. The FAQ
>> isn't geared to experienced users that can decipher code such as that
>> above, it is for people who *can't* write code such as that above but
>> want to learn from it.

>
> Where a FAQ entry is headed by a request for an algorithm, a coding
> tutorial is not called for. The Notes are the place for that.

Is there a point to that? I will repeat what I said "All of the code
snippets, every one of them, will be reviewed and *rewritten for 10.0*.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

krs
Guest
Posts: n/a

 01-16-2007

On Dec 13 2006, 2:32 pm, Dr J R Stockton <(E-Mail Removed)>
wrote:
> FAQ server <(E-Mail Removed)> wrote:
> The functions on my site have been updated since that was put in the
> FAQ.
>
> function Sign(X) { return X>0 ? "+" : X<0 ? "-" : " " }
>
> function PrfxTo(S, L, C) { S += ""
> if (C.length>0) while (S.length<L) S = C + S ; return S }
>
> function SpcsTo(S, L) { S += "" // SpcsTo is a reduction of PrfxTo
> while (S.length<L) S = " " + S ; return S }
>
> function StrU(X, M, N) { // X > -0.5e-N ; to M digits point N digits
> var S = String(Math.round(X*Math.pow(10, N)))
> if (/\D/.test(S)) return SpcsTo(X, M+N+1) // cannot cope
> S = PrfxTo(S, M+N, '0') ; var T = S.length - N
> return S.substring(0, T) + '.' + S.substring(T) }
>
> function StrS(X, M, N) { return Sign(X) + StrU(Math.abs(X), M, N) }
>
> Given that some of the function set are intended to give fixed-width
> results, ISTM that Sign should give fixed-width output. When that's
> *not* wanted, removing a bit of the code will be easy.

can someone else confirm that StrS(1.035,1,2) returns +1.03, instead of
the correct value of +1.04, on Firefox2 (winxp)?
-ks

Evertjan.
Guest
Posts: n/a

 01-16-2007
krs wrote on 16 jan 2007 in comp.lang.javascript:
> On Dec 13 2006, 2:32 pm, Dr J R Stockton
> <(E-Mail Removed)> wrote:
>> FAQ server <(E-Mail Removed)> wrote:
>> The functions on my site have been updated since that was put in the
>> FAQ.
>>
>> function Sign(X) { return X>0 ? "+" : X<0 ? "-" : " " }
>>
>> function PrfxTo(S, L, C) { S += ""
>> if (C.length>0) while (S.length<L) S = C + S ; return S }
>>
>> function SpcsTo(S, L) { S += "" // SpcsTo is a reduction of PrfxTo
>> while (S.length<L) S = " " + S ; return S }
>>
>> function StrU(X, M, N) { // X > -0.5e-N ; to M digits point N digits
>> var S = String(Math.round(X*Math.pow(10, N)))
>> if (/\D/.test(S)) return SpcsTo(X, M+N+1) // cannot cope
>> S = PrfxTo(S, M+N, '0') ; var T = S.length - N
>> return S.substring(0, T) + '.' + S.substring(T) }
>>
>> function StrS(X, M, N) { return Sign(X) + StrU(Math.abs(X), M, N) }
>>
>> Given that some of the function set are intended to give fixed-width
>> results, ISTM that Sign should give fixed-width output. When that's
>> *not* wanted, removing a bit of the code will be easy.

>
> can someone else confirm that StrS(1.035,1,2) returns +1.03, instead
> of the correct value of +1.04, on Firefox2 (winxp)?

Both FF2.0.0.1 and IE7 return:

+1.03

Why would +1.04 be the correct value?

--
Evertjan.
The Netherlands.

VK
Guest
Posts: n/a

 01-16-2007

Evertjan. wrote:
> > can someone else confirm that StrS(1.035,1,2) returns +1.03, instead
> > of the correct value of +1.04, on Firefox2 (winxp)?

>
> Both FF2.0.0.1 and IE7 return:
>
> +1.03
>
> Why would +1.04 be the correct value?

Because:
If the number you are rounding is followed by 5, 6, 7, 8, or 9, the
number has to be rounded up.
If the number you are rounding is followed by 0, 1, 2, 3, or 4, round
the number down.

If some other rounding logic implemented then it must be spelled in the

In this aspect the native toFixed method follows the common rounding
rules only on IE (JScript):

Both FF and Opera show 1.03

But alert(1.036.toFixed(2)); // show 1.04 on all test browsers

This way FF and Opera seems implementing an altered rounding rule:

If the number you are rounding is followed by 6, 7, 8, or 9, the number
has to be rounded up.
If the number you are rounding is followed by 0, 1, 2, 3, 4 or 5 round
the number down.

Could anyone comment on "the bugs in JScripts implementation" - what is
exactly meant by that?

Evertjan.
Guest
Posts: n/a

 01-16-2007
FAQ server wrote on 13 dec 2006 in comp.lang.javascript:

> -----------------------------------------------------------------------
> 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.
>
> 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 ? '-' : ''; }
> function StrS(X, M, N) { return Sign(X)+StrU(Math.abs(X), M, N) }
>
> Number.prototype.toFixed= new Function('n','return StrS(this,1,n)')

1
I would prefer to use regex when molding a string into shape:

<script type='text/javascript'>

function toFixed2prot(N) {
var re = new RegExp('(\d{'+N+'})\$','')
return Math.floor(this*Math.pow(10, N)+.5).toString()
.replace(/(\d{2}\$)/,'.\$1')
}

Number.prototype.toFixed2 = toFixed2prot

// testing:

var x = (1.0351).toFixed2(2)
document.write(x+'<br>')
var x = (1.035).toFixed2(2)
document.write(x+'<br>')

</script>

--
Evertjan.
The Netherlands.