2

I currently have some clean numbers like: 1, 10, 100, 1000, 10000 etc...

Im looking to format these numbers, so they appear as:

1,00
10,00
100,00
1.000,00
10.000,00

I played around with n.toFixed(2); but didn't seem to support the formatting im looking for.

Any ideas?

user1231561
  • 3,239
  • 6
  • 36
  • 55
  • JS has no native "fancy" number formatting functions. You'll have to roll your own, or find a pre-made library that does something like emulate sprintf() and the like. – Marc B Oct 10 '13 at 22:25
  • third-side script is acceptable solution? – Dvir Oct 10 '13 at 22:27
  • Anything could be acceptable, at the moment I have no idea how to do this - but this is a small script, so suddently having to include other big libraries would be a no-go - but im all ears on solutions :) – user1231561 Oct 10 '13 at 22:28
  • 2
    ["How to print a number with commas as thousands separators in JavaScript"](http://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript) has the answer you are looking for. – Ben Smith Oct 10 '13 at 22:38

5 Answers5

6

Check out Intl.NumberFormat(), no fancy pants plugins needed. Cross-platform support.

var number = 3500;
alert(new Intl.NumberFormat().format(number));

Will pop up '3,500'

synthesizerpatel
  • 27,321
  • 5
  • 74
  • 91
  • You can provide the `locales` parameter as well to get the commas and decimals swapped as the OP wants. `Intl.NumberFormat("de-DE")` Unfortunately, poor the browser support is not that great... [Support: Chrome 24, IE 11, Opera 15](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat#Browser_compatibility) – thgaskell Oct 10 '13 at 23:25
  • Since you linked Mozilla documentation I'll go out on a limb and guess Firefox is supported as well? – synthesizerpatel Oct 10 '13 at 23:37
  • 1
    From the Mozilla link: Firefox (Gecko), Safari (Webkit) - Not supported – Jasen Oct 11 '13 at 00:06
2

You can do it like this:

var nb='1000000000';
var result = nb.replace(/(?:(^\d{1,3})(?=(?:\d{3})*$)|(\d{3}))(?!$)/mg, '$1$2.')+',00';
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
2

If you want a simple function to do the job, the following may suit:

// Format a number n using: 
//   p decimal places (two by default)
//   ts as the thousands separator (comma by default) and
//   dp as the  decimal point (period by default).
//
//   If p < 0 or p > 20 results are implementation dependent.
function formatNumber(n, p, ts, dp) {
  var t = [];
  // Get arguments, set defaults
  if (typeof p  == 'undefined') p  = 2;
  if (typeof ts == 'undefined') ts = ',';
  if (typeof dp == 'undefined') dp = '.';

  // Get number and decimal part of n
  n = Number(n).toFixed(p).split('.');

  // Add thousands separator and decimal point (if requied):
  for (var iLen = n[0].length, i = iLen? iLen % 3 || 3 : 0, j = 0; i <= iLen; i+=3) {
    t.push(n[0].substring(j, i));
    j = i;
  }
  // Insert separators and return result
  return t.join(ts) + (n[1]? dp + n[1] : '');
}


//*
console.log(formatNumber(
    1234567890.567,  // value to format
                 4,  // number of decimal places
               '.',  // thousands separator
                ','  // decimal separator
 ));                 // result: 1.234.567.890,5670
//*/

console.log(formatNumber(
           123.567,  // value to format
                 1   // number of decimal places
 ));                 // result: 123.6

console.log(formatNumber(
         '123.567',  // value to format
                 0   // number of decimal places
 ));                 // result: 123.6

console.log(formatNumber(
               123,  // value to format
                 0   // number of decimal places
 ));                 // result: 123

console.log(formatNumber(
                13,  // value to format
                 2   // number of decimal places
 ));                 // result: 13.00

console.log(formatNumber(
                 0   // value to format
                     // number of decimal places
 ));                 // result: 0.00

console.log(formatNumber(
                     // value to format
                     // number of decimal places
 ));                 // result: NaN

Sorry, no fancy regular expressions or slice/splice array stuff, just POJS that works.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • I love stuff that works! and this is spot on and does exactly what I need it to do. The first console.log example did it for me. I love you! :) – user1231561 Oct 11 '13 at 12:03
-1

I don't know what your situation is, but i'd like to use angularJS.

It serves exactly what you are looking for and there are more extra dynamic formatting.

UPDATE: Specifically look into Localization functionality. It even serves pluralization if you need it.

Chris Wijaya
  • 1,276
  • 3
  • 16
  • 34
-1

There is no built-in methods for formatting numbers in JS. You can use autoNumeric for this. http://www.decorplanit.com/plugin/

Seth Malaki
  • 4,436
  • 23
  • 48