Javascript 获取区域设置的货币符号
如何获取给定货币字符串(“英镑”、“美元”等)的货币符号?我想出的最好的方法似乎冗长可笑,有没有其他方法,或者我最好使用查找表Javascript 获取区域设置的货币符号,javascript,locale,currency,symbols,Javascript,Locale,Currency,Symbols,如何获取给定货币字符串(“英镑”、“美元”等)的货币符号?我想出的最好的方法似乎冗长可笑,有没有其他方法,或者我最好使用查找表 constuserlocale=“EN-gb”; const userCurrency=“GBP”; const with valueel=document.querySelector(“.withValue”); const without valueel=document.querySelector(“.without value”); const valueWi
constuserlocale=“EN-gb”;
const userCurrency=“GBP”;
const with valueel=document.querySelector(“.withValue”);
const without valueel=document.querySelector(“.without value”);
const valueWithCurrency=Number(0)。toLocaleString(userLocale{
风格:“货币”,
货币:userCurrency,
最小分数位数:2
});
const valuewithout currency=valueWithCurrency.replace(数字(0))toLocaleString(用户语言环境{
最小分数位数:2
}), "")
withValueEl.innerHTML=valueWithCurrency
Without Valueel.innerHTML=不带货币的值
带值:
不带值:
这看起来比包含所有DOM选择器和值注入逻辑时实际需要做的工作要多,这与您想要的功能无关。提取符号非常简单:
const getCurrencySymbol = (locale, currency) => (0).toLocaleString(locale, { style: 'currency', currency, minimumFractionDigits: 0, maximumFractionDigits: 0 }).replace(/\d/g, '').trim()
或者,如果您没有使用es6:
function getCurrencySymbol (locale, currency) {
return (0).toLocaleString(
locale,
{
style: 'currency',
currency: currency,
minimumFractionDigits: 0,
maximumFractionDigits: 0
}
).replace(/\d/g, '').trim()
}
tolocalString
选项很冗长,因此没有什么可做的。但是您不需要使用Number
构造函数(实际上永远都不需要)。如果得到的货币值没有小数或分隔符,那么删除数字并只剩下符号就很简单了。您可能希望采用这种方法,因为根据区域设置和货币,符号并不总是单个字符。例如:
getCurrencySymbol('en-US', 'CNY') // CN¥
getCurrencySymbol('zh-CN', 'CNY') // ¥
修剪结果也是一个好主意。您可以将.trim()
链接到示例中的结果,或者更新正则表达式以包含空格。
应该注意,这是一种有点幼稚的方法,因为它只适用于数字字符0-9。如果需要包含其他数字字符,例如阿拉伯语(1632)、拉丁语(1636)、拉丁语(1638)、拉丁语(1640),则需要更新正则表达式以包含unicode范围:/[0-9\u0660-\u0669]/g
。您必须添加任何其他需要以类似方式支持的数字系统
本地化是一个非常重要的问题,因此仅使用货币代码来进行符号映射可能更有意义。这看起来比包含所有DOM选择器和值注入逻辑(与所需功能无关)时实际需要做的工作要多。提取符号非常简单:
const getCurrencySymbol = (locale, currency) => (0).toLocaleString(locale, { style: 'currency', currency, minimumFractionDigits: 0, maximumFractionDigits: 0 }).replace(/\d/g, '').trim()
或者,如果您没有使用es6:
function getCurrencySymbol (locale, currency) {
return (0).toLocaleString(
locale,
{
style: 'currency',
currency: currency,
minimumFractionDigits: 0,
maximumFractionDigits: 0
}
).replace(/\d/g, '').trim()
}
tolocalString
选项很冗长,因此没有什么可做的。但是您不需要使用Number
构造函数(实际上永远都不需要)。如果得到的货币值没有小数或分隔符,那么删除数字并只剩下符号就很简单了。您可能希望采用这种方法,因为根据区域设置和货币,符号并不总是单个字符。例如:
getCurrencySymbol('en-US', 'CNY') // CN¥
getCurrencySymbol('zh-CN', 'CNY') // ¥
修剪结果也是一个好主意。您可以将.trim()
链接到示例中的结果,或者更新正则表达式以包含空格。
应该注意,这是一种有点幼稚的方法,因为它只适用于数字字符0-9。如果需要包含其他数字字符,例如阿拉伯语(1632)、拉丁语(1636)、拉丁语(1638)、拉丁语(1640),则需要更新正则表达式以包含unicode范围:/[0-9\u0660-\u0669]/g
。您必须添加任何其他需要以类似方式支持的数字系统
本地化是一个非常重要的问题,所以只使用货币代码来符号映射可能更有意义。Intnl.NumberFormat#FormatParts
将返回一个格式化部分数组,包括一个看起来像{type:'currency',value:'$},
for USD的条目
因此,使用它,您可以:
const userLocale = "EN-gb";
const userCurrency = "GBP";
const withCurrency = new Intl.NumberFormat(userLocale, { style: 'currency', currency: userCurrency }).formatToParts(3.50).map(val => val.value).join('');
const withoutCurrency = new Intl.NumberFormat(userLocale, { style: 'currency', currency: userCurrency }).formatToParts(3.50).slice(1).map(val => val.value).join('');
并读取值:
> withCurrency
'£3.50'
> withoutCurrency
'3.50'
Intnl.NumberFormat#formatParts
将返回一个格式化部分数组,包括一个类似于{type:'currency',value:'$},
for USD的条目
因此,使用它,您可以:
const userLocale = "EN-gb";
const userCurrency = "GBP";
const withCurrency = new Intl.NumberFormat(userLocale, { style: 'currency', currency: userCurrency }).formatToParts(3.50).map(val => val.value).join('');
const withoutCurrency = new Intl.NumberFormat(userLocale, { style: 'currency', currency: userCurrency }).formatToParts(3.50).slice(1).map(val => val.value).join('');
并读取值:
> withCurrency
'£3.50'
> withoutCurrency
'3.50'
我可以建议你看看dinerojs吗?香草JS就是这样做的。为了使它更容易,您可以使用库。谷歌“javascript货币符号地图”@J.Pichardo我想不出如何更轻松地使用它dinerojs@Amy如果你想添加这个作为答案,我会接受。我可以建议你看看dinerojs吗?香草JS就是这样做的。为了使它更容易,您可以使用库。谷歌“javascript货币符号地图”@J.Pichardo我想不出如何更轻松地使用它dinerojs@Amy如果你想加上这个作为回答,我会接受的。