• Formatting numbers-as-strings in JavaScript

    Home » Forums » Developers, developers, developers » Web design and development » Formatting numbers-as-strings in JavaScript

    Author
    Topic
    #382372

    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:

    strFormatted = FormatNumber(dblVar, 2)

    JavaScript doesn’t seem to have anything like this, so I ended up doing the following. Is there a neater way?

    var strFormatted = Math.round(floatVar * 100).toString();
    strFormatted = strFormatted.substr(0, strFormatted.length - 2) + '.' +
      strFormatted.substr(strFormatted.length - 2, 2);

    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.

    Viewing 0 reply threads
    Author
    Replies
    • #648049

      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:

      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);
        }

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

      • #648070

        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

        • #648156

          Jefferson,

          For your case, we could just check to see if the result starts with “.” and prefix the zero:

            if (numFormatted.indexOf(".") == 0)
              numFormatted = "0" + numFormatted;
          

          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%5B/url%5D. But I think we covered the basics.

          HTH

    Viewing 0 reply threads
    Reply To: Formatting numbers-as-strings in JavaScript

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: