Why is Intl.NumberFormat formatting 4 digits together on es-ES locale? [duplicate]

半腔热情 提交于 2021-02-10 05:40:02

问题


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));

回答1:


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 €


来源:https://stackoverflow.com/questions/58430460/why-is-intl-numberformat-formatting-4-digits-together-on-es-es-locale

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!