获取语​​言环境的货币符号 [英] Get the currency symbol for a locale

查看:73
本文介绍了获取语​​言环境的货币符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取给定货币字符串("GBP","USD"等)的货币符号?我想出的最好的方法似乎太荒谬了,还有其他方法吗?还是我最好使用查找表?

How to get the currency symbol for a given currency string ("GBP", "USD" etc)? The best I've come up with seems ridiculously long winded, is there another way or am I better off with a lookup table?

const userLocale = "EN-gb";
const userCurrency = "GBP";
const withValueEl = document.querySelector(".withValue");
const withoutValueEl = document.querySelector(".withoutValue");
const valueWithCurrency = Number(0).toLocaleString(userLocale, {
  style: "currency",
  currency: userCurrency,
  minimumFractionDigits: 2
});
const valueWithoutCurrency = valueWithCurrency.replace(Number(0).toLocaleString(userLocale, {
  minimumFractionDigits: 2
}), "")

withValueEl.innerHTML = valueWithCurrency
withoutValueEl.innerHTML = valueWithoutCurrency

With value:<span class="withValue"></span><br /> Without value:<span class="withoutValue"></span><br />

推荐答案

这似乎比包含所有DOM选择器和值注入逻辑(与所需功能无关)的实际工作要多. .提取符号非常简单:

This looks like more work than it really is when you're including all of the DOM selector and value injection logic, which is not related to the functionality you want. Extracting the symbol is pretty straightforward:

const getCurrencySymbol = (locale, currency) => (0).toLocaleString(locale, { style: 'currency', currency, minimumFractionDigits: 0, maximumFractionDigits: 0 }).replace(/\d/g, '').trim()

或者如果您不使用es6:

Or if you aren't using es6:

function getCurrencySymbol (locale, currency) {
  return (0).toLocaleString(
    locale,
    {
      style: 'currency',
      currency: currency,
      minimumFractionDigits: 0,
      maximumFractionDigits: 0
    }
  ).replace(/\d/g, '').trim()
}

toLocaleString选项非常详细,因此没有太多要做.但是,您不需要使用Number构造函数(确实如此).如果获得的货币值不带小数点或分隔符,则删除数字并仅保留符号就很简单.您将要采用这种方法,因为根据语言环境和货币的不同,符号不一定总是单个字符.例如:

The toLocaleString options are verbose, so there's not much to do about that. But you don't need to use the Number constructor (really ever). If you get the currency value without decimals or separators it's simple to remove the numbers and have only the symbol left. You'll want to take this kind of approach because, depending on the locale and the currency, the symbol is not always a single character. For example:

getCurrencySymbol('en-US', 'CNY') // CN¥
getCurrencySymbol('zh-CN', 'CNY') // ¥

修剪结果也是一个好主意.您可以像示例中那样将.trim()链接到结果,或者更新正则表达式以包含空格. 应当指出,这是一种幼稚的方法,因为它仅适用于数字字符0-9.如果需要包含其他数字字符,例如阿拉伯(٠١٢٣٤٥٦٧٨٩),则需要更新正则表达式以包含unicode范围:/[0-9\u0660-\u0669]/g.您必须以类似的方式添加需要支持的任何其他号码系统.

Trimming the result is also a good idea. You can chain .trim() to the result like in the examples, or update the regex to include whitespaces. It should be noted this is a somewhat naive approach, since it only works for number characters 0-9. If you needed to include other number characters, such as Arabic (٠١٢٣٤٥٦٧٨٩), you'd need to update the regex to include unicode ranges: /[0-9\u0660-\u0669]/g. You'd have to add any other number system you need to support in similar fashion.

本地化不是一个简单的问题,因此仅使用货币代码对符号地图(例如

Localization is a non-trivial problem, so it might make more sense to just use a currency code to symbol map like this one.

这篇关于获取语​​言环境的货币符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆