FAQ Topic - How do I format a Number as a String with exactly 2 decimal places? (2008-11-12)

F

FAQ server

-----------------------------------------------------------------------
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.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*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 ` Number.prototype.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.round(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.prototype.toFixed = function(n){
return StrS(this, 1, n);
};

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

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.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.
 
D

disappearedng

What about multiplying by 100
and then cast (int) onto it,
then cast double (or whatever you need)
and divide by 100?

so
x = 6.43211
100x = 643.211
then (int) 643.211 = 643
then (double) 643 = 643
then / 100 = 6.43
 
D

disappearedng

What about multiplying by 100
and then cast (int) onto it,
then cast double (or whatever you need)
and divide by 100?

so
x = 6.43211
100x = 643.211
then (int) 643.211 = 643
then (double) 643 = 643
then / 100 = 6.43
 
D

Dr J R Stockton

In comp.lang.javascript message <2d662260-bd2e-46f2-bd3f-df7641fd7416@q3
0g2000prq.googlegroups.com>, Tue, 11 Nov 2008 17:40:00, disappearedng
What about multiplying by 100
and then cast (int) onto it,
then cast double (or whatever you need)
and divide by 100?

so
x = 6.43211
100x = 643.211
then (int) 643.211 = 643
then (double) 643 = 643
then / 100 = 6.43

What language might you be talking about there? If x is 6.4007, then
where are your two decimal places; if x=6.9997, where are the decimal
places? Where, on fact, is your String, as per Subject? Did you
actually understand the Subject?

All that your code can achieve is to convert a Number into one which is,
as nearly as possible, a multiple of 1/100.



FAQ Topic -

Because the FORTRAN convention is commonly, though not everywhere, used,
the identifier N in the paragraph starting "` N = Math.round(N*100)/100"
should be changed to be, say, X in 4 places.

In the Subject, 2 should be changed to N.

There is better code via the first link.

The FAQ does not say that, in StrU, N is the number of decimal places,
and M is the number of non-sign characters before the decimal point.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top