4

I'm trying to format a number using the Intl.NumberFormat.

I have checked MDN WebDocs but I'm not able to get the response I guess it should return.

I'm formatting with spanish locale, and I want to get the point separator between thousands (using useGrouping option), however, I'm not getting it

  • Expected result: 1.124,50 €
  • Obtained result: 1124,50 €

var sNumber = '1124.5'
var number = new Number(sNumber);

let  style = {
            style: 'currency',
            currency: "EUR",
            minimumFractionDigits: 2,
            useGrouping: true
        };

const formatter = new Intl.NumberFormat("es", style);

console.log(formatter.format(number));
fedorqui
  • 275,237
  • 103
  • 548
  • 598
Rafa Romero
  • 2,667
  • 5
  • 25
  • 51

1 Answers1

5

This seems to be a feature of the Spanish formatter with 4-digit numerics (i.e. 1234.56).

Take a look at the below and run it:

let  style = {
            style: 'currency',
            currency: "EUR",
            minimumFractionDigits: 2,
            useGrouping: true
        };
var formatter = new Intl.NumberFormat("es", style);

console.log('Spanish (ES)');
console.log(formatter.format(1234.56));
console.log(formatter.format(12345.67));
console.log(formatter.format(123456.78));

formatter = new Intl.NumberFormat("de-DE", style);

console.log('German (de-DE)');
console.log(formatter.format(1234.56));
console.log(formatter.format(12345.67));
console.log(formatter.format(123456.78));

You will see that for 5-digit and above numbers, the Spanish formatter does indeed group the numbers as expected.

However, if you use a German formatter (de-DE), it correctly formats the 4-digit numeric.

Output:

Spanish (ES)
1234,56 €
12.345,67 €
123.456,78 €

German (de-DE)
1.234,56 €
12.345,67 €
123.456,78 €
Martin
  • 16,093
  • 1
  • 29
  • 48
  • If this is correct then that means the Amazon website for Spain is displaying it wrong when they write 1099 as 1.099. https://www.amazon.es/s?k=macbook+pro – Rajiv Oct 07 '20 at 10:20
  • The point here is that spanish institution "RAE" stablishes that group separator shouldn't be used for 4 integer digit numbers → https://www.rae.es/dpd/n%C3%BAmeros – laconbass May 18 '21 at 13:45