15

Trying to format a number to two decimals format in European culture. So that comma is decimal separator and space thousands separator.

In example 213245 should be formatted as 213 245,00

How can I do that?

213245.toFixed(2).toLocaleString(); 

gives 213245.00 but it should be 213 245,00

however

213245.toLocaleString()

gives 213 245

Fiddling below:

var out, input;
input = 213245;

// TEST 1
out = input.toFixed(2);   
console.log(out); // 213245.00
out = out.toLocaleString();
console.log(out); // 213245.00

// TEST 2
out = input.toLocaleString();
console.log(out); // 213 245

https://jsfiddle.net/rootnode/8p2vad8u/7/

anmatika
  • 1,581
  • 3
  • 21
  • 29
  • http://stackoverflow.com/questions/6784894/add-commas-or-spaces-to-group-every-three-digits – adeneo Jun 11 '15 at 10:01
  • Are you sure that your locale is set correctly? – feeela Jun 11 '15 at 10:02
  • If you consider 213245.toFixed(2).toLocaleString(); as a string you can use replace() function to replace '.' with ',' – Mihai8 Jun 11 '15 at 10:06
  • 1
    Beware that [Number.toFixed()](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed) produces a string thus you're using [Object.toLocaleString()](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/toLocaleString) rather than [Number.toLocaleString()](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString) -- *Side note: it's for statements like this that Brits want to leave Europe...* – Álvaro González Jun 11 '15 at 10:10
  • @AlvaroG.Vicario _Side note: what? I don't want to start politically here, but you are seriously conflating things. The OP meant in comparison the American notation, which the Brits happened to adopt_ – somethinghere Jun 11 '15 at 10:53
  • @somethinghere Come on, do I really need to add an smiley to every single tongue-in-cheek sentence I say on the internet? – Álvaro González Jun 11 '15 at 11:00
  • 2
    @AlvaroG.Vicario _Sorry about that! I guess it's a politically-sensitive topic :)_ Now, back to the question: theres actually a useful implementation here: http://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-javascript - this one is for formatting money, but it works for decimals as well. – somethinghere Jun 11 '15 at 11:03

1 Answers1

39

When you use Number.toFixed() you obtain a string (not a number any more). For that reason, subsequent calls to .toLocaleString() launch the generic Object.toLocaleString() method that knows nothing about numbers, instead of the Number.toLocaleString() you want.

Having a look at the documentation we can compose something like this:

> Number(213245).toLocaleString("es-ES", {minimumFractionDigits: 2});
"213.245,00"

console.log(Number(213245).toLocaleString("es-ES", {minimumFractionDigits: 2}));

This is a relatively new addition so make sure to verify browser support, but it's been working in Firefox and Chrome-like browsers for a few years now. In particular, some runtimes like Node.js do not include the full ICU dataset by default.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360