Friday, April 12, 2013

Format Numbers Using JavaScript

Hello friends,

Today in Developer's Blog, I will tell you the way to Format Numbers Using JavaScript.

We already have a function to format numbers in PHP, which is number_format(number,decimal_places).

So we also need a function which can format numbers in JavaScript, because a lot of times we need to calculate the cost or something and show it formatted using JavaScript.

So here's the function which will format your numbers using JavaScript.

function number_format(str) {
    var amount = new String(str);
    amount = amount.split("").reverse();

    var output = "";
    for ( var i = 0; i <= amount.length-1; i++ ){
        output = amount[i] + output;
        if ((i+1) % 3 == 0 && (amount.length-1) !== i)output = ',' + output;
    return output;

So by this you will be able to Format Numbers Using JavaScript.

I hope this will help you and you will like it.

Don't forget to leave your comments.

Thank you


  1. This is very useful function written in js.
    keep your good work.

  2. Awesome function man. You made me follow your blog now. Thanks for this function.