You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用JavaScript获取货币名称的复数形式?

获取货币名称的复数形式

你已经知道可以用Intl.DisplayNames获取货币的单数名称(例如US Dollar),但Intl.DisplayNames本身不支持直接返回复数形式。要得到复数形式的货币名(比如US Dollars),可以借助Intl.NumberFormat实现——它会根据不同语言的复数规则自动生成正确的复数形式。

示例代码

// 定义函数获取复数货币名称
const getPluralCurrencyName = (currencyCode, locale = 'en-US') => {
  // 配置NumberFormat,格式化大于1的数值以触发复数规则
  const formatter = new Intl.NumberFormat(locale, {
    style: 'currency',
    currency: currencyCode,
    minimumFractionDigits: 0,
    maximumFractionDigits: 0
  });
  // 提取格式结果中的货币名称部分
  const parts = formatter.formatToParts(2);
  return parts.find(part => part.type === 'currency').value;
};

// 测试示例
console.log(getPluralCurrencyName('USD'));
// 输出: "US Dollars"

console.log(getPluralCurrencyName('EUR', 'fr-FR'));
// 输出: "euros"

原理说明

Intl.NumberFormat在格式化货币时,会根据传入的数值(这里用2确保触发复数逻辑)和目标语言的复数规则,自动生成对应复数形式的货币名称。通过formatToParts方法可以拆分格式化结果的各个组成部分,从而提取出纯复数货币名。

内容的提问来源于stack exchange,提问作者Scott

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 22:59:59