Intl.NumberFormat returns 数字和货币符号用空格分隔的结果
Intl.NumberFormat returns a result where number and currency symbol are divided through spacing
我刚刚为我的 vue-app 添加了一个过滤器,它应该将一个值格式化为欧元货币(德语格式)。
这是函数:
app.config.globalProperties.$filters = {
currency(value) {
return new Intl.NumberFormat(process.env.MIX_LOCALE, { style: 'currency', currency: 'EUR' }).format(value);
},
}
如果我现在做类似的事情
{{$filters.currency(17.85)}}
我得到这个结果:17,85 €
如您所见,数值和货币符号之间隔着一个space。这与我们过去在德国看到的不太一样,我们希望 17,85€
.
因为我没有在 Intl.NumberFormat
的文档中找到任何有用的信息,所以我想知道是否可以使用 Intl.NumberFormat
.
删除 space
如您在此 ticket here 中所见,该问题已为 Intl 所知,并且仍未解决。
你能做的是this。
我的第一反应是,为什么不replace()
?然后我看到了另一个答案的link。然后我试了一下,正则表达式不起作用,space 仍然存在。所以这里有一个有效的替换来修复这个错误。
app.config.globalProperties.$filters = {
currency(value) {
return new Intl.NumberFormat(
process.env.MIX_LOCALE,
{style: 'currency', currency: 'EUR'})
.format(value).replace(/\s/g,'');
}
}
可测试的片段
console.log(new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(1234567).replace(/\s/g,''));
我刚刚为我的 vue-app 添加了一个过滤器,它应该将一个值格式化为欧元货币(德语格式)。
这是函数:
app.config.globalProperties.$filters = {
currency(value) {
return new Intl.NumberFormat(process.env.MIX_LOCALE, { style: 'currency', currency: 'EUR' }).format(value);
},
}
如果我现在做类似的事情
{{$filters.currency(17.85)}}
我得到这个结果:17,85 €
如您所见,数值和货币符号之间隔着一个space。这与我们过去在德国看到的不太一样,我们希望 17,85€
.
因为我没有在 Intl.NumberFormat
的文档中找到任何有用的信息,所以我想知道是否可以使用 Intl.NumberFormat
.
如您在此 ticket here 中所见,该问题已为 Intl 所知,并且仍未解决。
你能做的是this。
我的第一反应是,为什么不replace()
?然后我看到了另一个答案的link。然后我试了一下,正则表达式不起作用,space 仍然存在。所以这里有一个有效的替换来修复这个错误。
app.config.globalProperties.$filters = {
currency(value) {
return new Intl.NumberFormat(
process.env.MIX_LOCALE,
{style: 'currency', currency: 'EUR'})
.format(value).replace(/\s/g,'');
}
}
可测试的片段
console.log(new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(1234567).replace(/\s/g,''));