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

Discussion in 'Javascript' started by FAQ server, Oct 16, 2006.

1. ### FAQ serverGuest

-----------------------------------------------------------------------
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-us/script56/html/js56jsmthToFixed.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.

FAQ server, Oct 16, 2006

2. ### PanosGuest

How about this ...

function round_me(n, d){
if (d<0) d = 0;
return Math.round(n * Math.pow(10,d))/Math.pow(10,d);
}

round_me(123.06467,2) = 123.06
round_me(0.06467,3) = 0.065
round_me(-0.06467,3) = -0.065

looks like it's working for me ... or do I need to learn more?!

FAQ server wrote:
> -----------------------------------------------------------------------
> 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-us/script56/html/js56jsmthToFixed.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.

Panos, Nov 2, 2006

3. ### Dr J R StocktonGuest

In message <>, Thu,
2 Nov 2006 07:25:44, Panos <> writes
>How about this ...
>
>function round_me(n, d){
> if (d<0) d = 0;
> return Math.round(n * Math.pow(10,d))/Math.pow(10,d);
> }
>
>round_me(123.06467,2) = 123.06
>round_me(0.06467,3) = 0.065
>round_me(-0.06467,3) = -0.065
>
>looks like it's working for me ... or do I need to learn more?!

You need to read the FAQ on not top-posting nor over-quoting.

You need to read the first paragraph of that FAQ entry more carefully.
You need to read the third paragraph of that FAQ entry.

You could read the references cited in that part of the FAQ, too.

typeof round_me(3,2) -> number
round_me(3,2) -> 3

You have rounded the Number n to be a multiple of 10^-d, which, as
discussed in js-round.htm ff., is not at all the same thing as rounding
to a string with two decimal places, such as 3.00 .

Your code could perhaps be made more efficient by not calculating 10^d
twice.

<FAQENTRY>
The code in the FAQ entry is not exactly what I would now recommend. The
FAQ does not say what M & N are.

The FAQ has an entry for the Flanagan books; the small Flanagan is good
on the methods of the Javascript objects. Is there a *similarly
concise* Web page which is good on the DOM methods, and could the FAQ
cite it??

See below.

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

Dr J R Stockton, Nov 2, 2006
4. ### PanosGuest

Dr J R Stockton wrote:
> In message <>, Thu,
> 2 Nov 2006 07:25:44, Panos <> writes
> >How about this ...
> >
> >function round_me(n, d){
> > if (d<0) d = 0;
> > return Math.round(n * Math.pow(10,d))/Math.pow(10,d);
> > }
> >
> >round_me(123.06467,2) = 123.06
> >round_me(0.06467,3) = 0.065
> >round_me(-0.06467,3) = -0.065
> >
> >looks like it's working for me ... or do I need to learn more?!

>
>
> You need to read the FAQ on not top-posting nor over-quoting.
>
> You need to read the first paragraph of that FAQ entry more carefully.
> You need to read the third paragraph of that FAQ entry.

I should read the FAQ ;-)

>
> You could read the references cited in that part of the FAQ, too.
>
> typeof round_me(3,2) -> number
> round_me(3,2) -> 3
>
> You have rounded the Number n to be a multiple of 10^-d, which, as
> discussed in js-round.htm ff., is not at all the same thing as rounding
> to a string with two decimal places, such as 3.00 .

ok ..

function round_me(n, d){
if (d<0) d = 0;
a=""+Math.round(n * Math.pow(10,d))/Math.pow(10,d);
p=a.search(/\./);
if (p == -1) { a = a+"."; p=1; }
for(i=a.substr(p).length-1;i<d;i++) { a = a+"0"; }
return a;
}

if I change the function a bit I get:

typeof round_me(3,2) -> string
round_me(3,2) -> 3.00

Panos, Nov 3, 2006
5. ### RusoGuest

I use this round function, which I found somewhere over the internet:

function roundit(Num, Places) { //Function to round the value --- used
to round the %
if (Places > 0) {
if ((Num.toString().length - Num.toString().lastIndexOf('.')) >
(Places + 1)) {
var Rounder = Math.pow(10, Places);
return Math.round(Num * Rounder) / Rounder;
}
else return Num;
}
else return Math.round(Num);
}

How to use it:

roundit(Value, to which decimal)
for example
roundit(100.35321,2) = 100.35

Panos wrote:
> Dr J R Stockton wrote:
> > In message <>, Thu,
> > 2 Nov 2006 07:25:44, Panos <> writes
> > >How about this ...
> > >
> > >function round_me(n, d){
> > > if (d<0) d = 0;
> > > return Math.round(n * Math.pow(10,d))/Math.pow(10,d);
> > > }
> > >
> > >round_me(123.06467,2) = 123.06
> > >round_me(0.06467,3) = 0.065
> > >round_me(-0.06467,3) = -0.065
> > >
> > >looks like it's working for me ... or do I need to learn more?!

> >
> >
> > You need to read the FAQ on not top-posting nor over-quoting.
> >
> > You need to read the first paragraph of that FAQ entry more carefully.
> > You need to read the third paragraph of that FAQ entry.

>
> I should read the FAQ ;-)
>
> >
> > You could read the references cited in that part of the FAQ, too.
> >
> > typeof round_me(3,2) -> number
> > round_me(3,2) -> 3
> >
> > You have rounded the Number n to be a multiple of 10^-d, which, as
> > discussed in js-round.htm ff., is not at all the same thing as rounding
> > to a string with two decimal places, such as 3.00 .

>
> ok ..
>
> function round_me(n, d){
> if (d<0) d = 0;
> a=""+Math.round(n * Math.pow(10,d))/Math.pow(10,d);
> p=a.search(/\./);
> if (p == -1) { a = a+"."; p=1; }
> for(i=a.substr(p).length-1;i<d;i++) { a = a+"0"; }
> return a;
> }
>
> if I change the function a bit I get:
>
> typeof round_me(3,2) -> string
> round_me(3,2) -> 3.00

Ruso, Nov 3, 2006
6. ### Dr J R StocktonGuest

In message <>, Fri,
3 Nov 2006 02:02:40, Panos <> writes
>function round_me(n, d){
> if (d<0) d = 0;
> a=""+Math.round(n * Math.pow(10,d))/Math.pow(10,d);
> p=a.search(/\./);
> if (p == -1) { a = a+"."; p=1; }
> for(i=a.substr(p).length-1;i<d;i++) { a = a+"0"; }
> return a;
> }

Fails with round_me(1e22, 2)
round_me(9.999e18, 6)
round_me(999, 6)

Before re-inventing the wheel, one should endeavour to understand it,
fully.

It's a good idea to read the newsgroup and its FAQ. See below.

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

Dr J R Stockton, Nov 3, 2006
7. ### Dr J R StocktonGuest

In message <>, Fri,
3 Nov 2006 06:46:58, Ruso <> writes
>I use this round function, which I found somewhere over the internet:

That's a good way of finding bad code.

>function roundit(Num, Places) { //Function to round the value --- used
>to round the %
> if (Places > 0) {
> if ((Num.toString().length - Num.toString().lastIndexOf('.')) >
>(Places + 1)) {
> var Rounder = Math.pow(10, Places);
> return Math.round(Num * Rounder) / Rounder;
> }
> else return Num;
> }
> else return Math.round(Num);
>}

Don't allow your sending agent to wrap code.

Your function returns a Number, not a string.

roundit(3.5, 2) -> 3.5

3.5 has only one decimal place; for exactly two, the answer would be
string "3.50".

It's a good idea to read the newsgroup and its FAQ. See below.

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

Dr J R Stockton, Nov 3, 2006

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.