问题描述
我正在尝试使用 Intl.NumberFormat 格式化数字.
I'm trying to format a number using the Intl.NumberFormat.
我已经检查了 MDN WebDocs 但我无法获得我认为应该返回的响应.
I have checked MDN WebDocs but I'm not able to get the response I guess it should return.
我正在使用西班牙语语言环境进行格式化,我想获得成千上万的点分隔符(使用 useGrouping
选项),但是,我没有得到
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
- 预期结果:1.124,50€
- 获得的结果: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));
推荐答案
这似乎是具有4位数字(即1234.56)的西班牙格式程序的功能.
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));
您会看到,对于5位数及以上的数字,西班牙语格式化程序确实确实按预期对数字进行了分组.
You will see that for 5-digit and above numbers, the Spanish formatter does indeed group the numbers as expected.
但是,如果您使用德语格式化程序( de-DE
),它将正确格式化4位数字.
However, if you use a German formatter (de-DE
), it correctly formats the 4-digit numeric.
输出:
Spanish (ES)
1234,56 €
12.345,67 €
123.456,78 €
German (de-DE)
1.234,56 €
12.345,67 €
123.456,78 €
这篇关于为什么Intl.NumberFormat在es-ES语言环境中一起格式化4位数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!