Math Jazz — Mathias Bynens’s shizzle, y’all



Note: This site might seem inactive… That’s because it is. Don’t worry though, I’m still coding webpages and stuff! If you’re interested, I suggest you get a translator and head over to Qiwi; or you could just check the latest site we’ve been working on: Apotheek Goethals – Debrabandere. Enjoy!

Formatting a number with grouped thousands

In PHP, formatting a number with grouped thousands can easily peasily be done through the number_format() function. It’s a shame JavaScript doesn’t have such a function.

That’s where this script comes in handy. It has the same syntax as the PHP function it’s spoofing, but adds two more parameters.

function number_format(a, b, c, d) {
 // number_format(number, decimals, comma, formatSeparator)
 var k = '';
 if(a.indexOf('-') == 0) {
  k = '-';
  a = a * -1;
 }
 a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
 var e = a + '';
 var f = e.split('.');
 if(!f[0]) f[0] = '0';
 if(!f[1]) f[1] = '';
 if(f[1].length < b){
  var g = f[1];
  for(var i = f[1].length + 1; i <= b; i++) {
   g += '0';
  }
  f[1] = g;
 }
 if(d != '' && f[0].length > 3) {
  var h = f[0];
  f[0] = '';
  for(var j = 3; j < h.length; j += 3) {
   i = h.slice(h.length - j, h.length - j + 3);
   f[0] = d + i +  f[0] + '';
  }
  j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
  f[0] = j + f[0];
 }
 c = (b <= 0) ? '': c;
 return k + f[0] + c + f[1];
}

These two examples should illustrate how it works. And if they don’t — here’s a nice test suite annex demo page of the script in action.

Update: Thanks to Alessandro Lia for pointing out a bug regarding negative numbers. This is fixed now.

Filed under JavaScript, PHP · January 8th, 2006

Comments (11)

Listed below are the responses for this entry.

  1. Eddy:
    This commenter’s Gravatar

    Thank you for sharing your script. I use it in the calculator for my website.

    Comment posted on January 14th, 2006 @ 7:58 am
  2. Sébastien Cosson:
    This commenter’s Gravatar

    A very big thanks for your script. I’ll use it in a calculator of diameters of organ pipes. It’s absolutely (maybe better) what I searched for!

    Comment posted on April 1st, 2006 @ 9:54 pm
  3. Edd:
    This commenter’s Gravatar

    It’s a big help for me too! Thanks!

    Comment posted on June 2nd, 2006 @ 4:08 am
  4. Steve Webster:
    This commenter’s Gravatar

    Great function, but has this code been obfuscated? I can’t think why you’d purposely name your variables starting with the letter a and moving on through the alphabet.

    Comment posted on June 7th, 2006 @ 10:01 am
  5. Yossli:
    This commenter’s Gravatar

    Super script, thx

    Comment posted on June 9th, 2006 @ 10:10 pm
  6. Mathias:
    This commenter’s Gravatar

    Great function, but has this code been obfuscated? I can’t think why you’d purposely name your variables starting with the letter a and moving on through the alphabet.

    It’s short and sweet.

    Comment posted on June 14th, 2006 @ 6:32 pm
  7. Robert Moreno:
    This commenter’s Gravatar

    Good script, thank you!

    Comment posted on July 4th, 2006 @ 12:38 am
  8. Ale:
    This commenter’s Gravatar

    Great function!!! I just have a little problem with negative numbers with 3, 6, 9… digits.

    I tried these two examples:

    • number_format('1234', 2, ',', '.') and the result is “1.234,00″ → correct
    • number_format('-1234', 2, ',', '.') and the result is “-1.234,00″ → correct

    …but…

    • number_format('123', 2, ',', '.') and the result is “123,00″ → correct
    • number_format('-123', 2, ',', '.') and the result is “-.123,00″ → ERROR

    Help appreciated, have a nice day!

    Comment posted on October 16th, 2007 @ 8:28 am
  9. Ale:
    This commenter’s Gravatar

    I think you’d like this version, that seems to solve the negative issue… Sorry if I did it without your permission, hope you appreciate it!

    
    function number_format(a, b, c, d) {
      // number_format(number, decimals, comma, formatSeparator)
      // 20071016 - alessandro.lia@gruppotesi.com
      // negative number in input → convert to positive and store the minus sign for the future!
      my_sign = '';
      if(a.indexOf('-') == 0){
        my_sign = '-';
        a = a * -1;
      }
      a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
      e = a + '';
      f = e.split('.');
      if(!f[0]) f[0] = '0';
      if(!f[1]) f[1] = '';
      if(f[1].length  3) {
        h = f[0];
        f[0] = '';
        for(j = 3; j 
    Comment posted on October 16th, 2007 @ 9:42 am
  10. Gregory:
    This commenter’s Gravatar

    This is exactly what javascript needs… well, not anymore, your script does the trick! Thank you so much for sharing!

    Comment posted on December 3rd, 2007 @ 9:38 pm
  11. Mathias:
    This commenter’s Gravatar

    Great function!!! I just have a little problem with negative numbers with 3, 6, 9… digits.

    Thanks for pointing that out, Ale! I see you tried to post a fix, but somehow part of your code got lost along the way.

    Anyhow, the script was updated to reflect this issue.

    Comment posted on June 8th, 2008 @ 1:18 pm