问题
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