Printing formatted decimal numbers

A

Aaron Gray

I need to print formatted decimal numbers to a set number of decimal places.

Is there a way to do this with JavaScript ?

If not is there any existing/"library" functions to do this ?

Many thanks in advance,

Aaron
 
J

Joshie Surber

Aaron said:
I need to print formatted decimal numbers to a set number of decimal places.

Is there a way to do this with JavaScript ?

If not is there any existing/"library" functions to do this ?

Many thanks in advance,

Aaron

pi = 3.14159265;
x = n.toFixed(2);
// x == 3.14
y = n.toFixed(4);
// y == 3.1416

Note that this is only in JS1.5 and ECMAv3, but that is still most
everything released this century.
 
R

Randy Webb

Joshie Surber said the following on 6/16/2006 9:04 PM:
pi = 3.14159265;
x = n.toFixed(2);
// x == 3.14
y = n.toFixed(4);
// y == 3.1416

Note that this is only in JS1.5 and ECMAv3, but that is still most
everything released this century.

Also note that toFixed() is severely crippled in IE with it's bug. Read
the FAQ with regards to formatting a number with leading and/or trailing
zeroes.

Convert it to a string, pad it, print the string.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top