Format Currency

This function will round numbers to two decimal places, and ensure that the returned value has two decimal places. For example 12.006 will return 12.01, .3 will return 0.30, and 5 will return 5.00

function CurrencyFormatted(amount) {
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

Comments

  1. User Avatar
    Chase Moskal
    Permalink to comment#

    If you want a smaller function:

    
    function currency(N){N=parseFloat(N);if(!isNaN(N))N=N.toFixed(2);else N='0.00';return N;}
    
    // >> currency("-14"): "-14.00" 
    

    If you want a smaller, more inline alternative, you can do this:

    
    // Right after you declare your number (N), the rest of the line does the formatting.
    var N=24.686; N=parseFloat(N);if(!isNaN(N))N=N.toFixed(2);else N='0.00';
    // >> N: "24.69"
    

    -Chase

    • User Avatar
      Chase Moskal
      Permalink to comment#

      Hey, here’s myself from the future. I re-gazed upon this, and further improved things:

      function currency(n){n=parseFloat(n);return isNaN(n)?false:n.toFixed(2);}

      currency(2); //--> '2.00'
      currency(3.253); //--> '3.25'
      currency(3.257); //--> '3.26'
      
      currency("3.2"); //--> '3.20'
      currency("butts"); //--> false
      
  2. User Avatar
    Andras
    Permalink to comment#

    Is it a css trick?

  3. User Avatar
    Thang
    Permalink to comment#

    Hi all!!

    Is there any way to add comma while typing number? and we can edit all of number.

    Ex: 123,456,555, we can edit number 1 –> 6

    Thanks

    • User Avatar
      Paul C Allsopp
      Permalink to comment#

      On keyup check the length of the field. If it is greater than 4 and contains no period, add a comma after every 3rd number from the right. When a period is encountered, set a flag to stop the formatting.

    • User Avatar
      Michael Max
      Permalink to comment#

      // with comma for thousands
      function format_currency(amount){
      var i = parseFloat(amount);
      if(isNaN(i)) { i = 0.00; }
      var minus = ”;
      if(i < 0) { minus = ‘-‘; }
      i = Math.abs(i);
      i = parseInt((i + .005) * 100);
      i = i / 100;
      s = Number(i).toLocaleString(‘en’);
      if(s.indexOf(‘.’) < 0) { s += ‘.00’; }
      if(s.indexOf(‘.’) == (s.length – 2)) { s += ‘0’; }
      s = minus + s;
      return s
      }

  4. User Avatar
    Manie
    Permalink to comment#

    The website’s name is css-tricks but you give a Java solution?

  5. User Avatar
    mahesh
    Permalink to comment#

    thanks
    i want to put auto price changer to my website

    please review my website: http://www.phonegallery.in

  6. User Avatar
    techrex
    Permalink to comment#

    Personally, I prefer to use currencyFormatter.js (https://osrec.github.io/currencyFormatter.js/). Cross browser and light. Can format numbers according to currency and/or locales:

    OSREC.CurrencyFormatter.format(2534234, { currency: 'INR' }); // Returns ₹ 25,34,234.00
    OSREC.CurrencyFormatter.format(2534234, { currency: 'EUR' }); // Returns 2.534.234,00 €
    OSREC.CurrencyFormatter.format(2534234, { currency: 'EUR', locale: 'fr' }); // Returns 2 534 234,00 €
    

Submit a Comment

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

icon-anchoricon-closeicon-emailicon-linkicon-logo-staricon-menuicon-nav-guideicon-searchicon-staricon-tag