Velocity Reviews > Printing formatted decimal numbers

# Printing formatted decimal numbers

Aaron Gray
Guest
Posts: n/a

 06-16-2006
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

Joshie Surber
Guest
Posts: n/a

 06-17-2006

Aaron Gray wrote:
> 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.

Randy Webb
Guest
Posts: n/a

 06-17-2006
Joshie Surber said the following on 6/16/2006 9:04 PM:
> Aaron Gray wrote:
>> 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.

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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

Dr John Stockton
Guest
Posts: n/a

 06-18-2006
JRS: In article <(E-Mail Removed)>, dated Fri, 16 Jun
2006 21:46:09 remote, seen in news:comp.lang.javascript, Aaron Gray
<(E-Mail Removed)> posted :
>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 ?

Read the newsgroup FAQ, and <URL:http://www.merlyn.demon.co.uk/js-
round.htm> ff.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<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.