Results 1 to 4 of 4
  1. #1
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Formatting numbers-as-strings in JavaScript

    Yesterday I was trying to figure out how to format numbers such as 71.75, 99.8, and 111.699999999999 as n.nn strings. In VBScript, you can do this:

    <pre>strFormatted = FormatNumber(dblVar, 2)</pre>

    JavaScript doesn't seem to have anything like this, so I ended up doing the following. Is there a neater way?
    <pre>var strFormatted = Math.round(floatVar * 100).toString();
    strFormatted = strFormatted.substr(0, strFormatted.length - 2) + '.' +
    strFormatted.substr(strFormatted.length - 2, 2);</pre>

    Notes:
    1. In real life, there is no break in that last line.
    2. I realize that for values of floatVar less than 1, I won't get a leading zero. I didn't have to worry about that for the demo page I was building.

  2. #2
    2 Star Lounger
    Join Date
    Apr 2001
    Location
    Coppell, Texas, USA
    Posts
    168
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Formatting numbers-as-strings in JavaScript

    You've pretty much nailed it. JavaScript isn't very helpful with formattig, leaving you to roll your own.

    A simple version for JavaScript might be like this:
    <pre>function formatNumber(numFloat, n)
    {
    if (n < 1) return alert("number of places (n) must be greater than 0!");

    var s, t;
    var numFormatted = (s=new String(Math.round(numFloat*Math.pow(10,n))))
    .substr(0, t=(s.length-n)) + '.' + s.substr(t, n);
    return (numFormatted);
    }</pre>

    Probably doesn't work exactly like VB's, but it's a start. HTH

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Formatting numbers-as-strings in JavaScript

    Thanks!

    First, I have to say I am totally amazed that JavaScript lets you declare and assign a new variable in the middle of an expression and then use that variable later in the expression (during the next step in its evaluation). This is fabulously efficient, but I can see really confusing myself with this syntax.

    Second, can I take you one step further down the road? Is there an efficient way to put some leading zeroes on the string if numFloat is less than 1? I'm guessing we need to do this before taking the substrings...that is, if s.length == n then tack a zero on the front, if s.length < n then more than one zero will be needed...

    Example for n=2:

    1.5 formats to 1.50
    0.8 formats to 0.80
    0.048 formats to 0.05

  4. #4
    2 Star Lounger
    Join Date
    Apr 2001
    Location
    Coppell, Texas, USA
    Posts
    168
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Formatting numbers-as-strings in JavaScript

    Jefferson,

    For your case, we could just check to see if the result starts with "." and prefix the zero:
    <pre> if (numFormatted.indexOf(".") == 0)
    numFormatted = "0" + numFormatted;
    </pre>

    before the return.

    I amazed that JavaScript lets you write whole functions inline and use them. But then I think Java is like that too. Looks like I might finally get to work in Java for my job, althought we thought that would happen a couple years back.

    I did a quick Google search and came up with lots of serious number formatting scripts. I downloaded one that looks promising from http://www.mredkj.com/javascript/numberFormat.html. But I think we covered the basics.

    HTH

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •