Printing formatted decimal numbers

Discussion in 'Javascript' started by Aaron Gray, Jun 16, 2006.

  1. Aaron Gray

    Aaron Gray Guest

    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
    Aaron Gray, Jun 16, 2006
    #1
    1. Advertising

  2. 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.
    Joshie Surber, Jun 17, 2006
    #2
    1. Advertising

  3. Aaron Gray

    Randy Webb Guest

    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/
    Randy Webb, Jun 17, 2006
    #3
  4. JRS: In article <>, dated Fri, 16 Jun
    2006 21:46:09 remote, seen in news:comp.lang.javascript, Aaron Gray
    <> 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.
    Dr John Stockton, Jun 18, 2006
    #4
    1. Advertising

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.
Similar Threads
  1. Stuart Woodard

    Server-side Printing With Formatted Text

    Stuart Woodard, Aug 3, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    312
    Stuart Woodard
    Aug 3, 2003
  2. KB
    Replies:
    3
    Views:
    20,817
    Pabinator
    Aug 28, 2009
  3. Replies:
    2
    Views:
    483
  4. Wink Saville
    Replies:
    5
    Views:
    99
    Austin Ziegler
    Mar 20, 2006
  5. Mark Hobley

    printing long decimal numbers

    Mark Hobley, Jan 3, 2007, in forum: Perl Misc
    Replies:
    1
    Views:
    76
    DJ Stunks
    Jan 3, 2007
Loading...

Share This Page