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.
number_format(1234567890, 0, '.', ',')would return 1,234,567,890.number_format(1234567890.119, 2, ',', '.')would return 1.234.567.890,12.
Update: Thanks to Alessandro Lia for pointing out a bug regarding negative numbers. This is fixed now.
Comments (11)
Listed below are the responses for this entry.
Thank you for sharing your script. I use it in the calculator for my website.
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!
It’s a big help for me too! Thanks!
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.
Super script, thx
It’s short and sweet.
Good script, thank you!
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″ → correctnumber_format('-1234', 2, ',', '.')and the result is “-1.234,00″ → correct…but…
number_format('123', 2, ',', '.')and the result is “123,00″ → correctnumber_format('-123', 2, ',', '.')and the result is “-.123,00″ → ERRORHelp appreciated, have a nice day!
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!
This is exactly what javascript needs… well, not anymore, your script does the trick! Thank you so much for sharing!
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.