@checkdigit/currency

中文文档
9.0.0发布于 2026/09/09MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 2752

Standard currency handling

原始使用文档
文档目录17 节
  1. Installing
  2. Types and interfaces
  3. Functions
  4. Formatting
  5. Parsing
  6. Currencies
  7. Countries
  8. Usage examples
  9. format
  10. parse
  11. getSymbol
  12. getMinorUnitDigits
  13. findCurrency
  14. getCurrency
  15. getCountry
  16. getCountriesForCurrency
  17. License

Check Digit Currency Library

Copyright © 2021–2026 Check Digit, LLC

The Check Digit currency library is the officially sanctioned method for Check Digit services to deal with currency types, formatting and country/currency relationships at a particular date/time. Features:

  • various currency and country lookup functions at a particular date/time starting 2018 and beyond, anything earlier will throw an error.
  • TypeScript types for Amount, Money, ISO 3166 country codes (numeric, alpha2, alpha3), and ISO 4217 currency codes (name, alphabetic, numeric)
  • currency formatting of Check Digit-standard Money objects, with a variety of options
  • currency parsing of strings into Check Digit-standard Money objects
  • tests to ensure compliance with number-based Intl.NumberFormat currency implementation
  • tests to ensure correctness of underlying JS engine Intl implementation, with respect to currency
  • multi-locale (all modern browsers and Node 24+ include full ICU)
  • uses built-in JS engine Intl implementation, no dependencies

Installing

npm install @checkdigit/currency

then:

import * as currency from '@checkdigit/currency';

Types and interfaces

The four core types in the currency library are

  • Amount, which is an integer either in bigint or string form (or -0).
  • Money, which is an Amount combined with currency of type CurrencyAlphabeticCode (e.g. 'USD')
  • Country, which is an object containing a country's ISO 3166 information and currencies
  • Currency, which is an object containing a currency's ISO 4217 information

There are defined literal types for country and currency codes:

  • CurrencyAlphabeticCode
  • CurrencyNumericCode
  • CurrencyName
  • CountryAlpha2
  • CountryAlpha3
  • CountryNumeric
export interface Money {
  amount: string; // integer amount in minor units, e.g. '12345' for $123.45
  currency: CurrencyAlphabeticCode;
}

export interface Country {
  // ISO 3166 country display name, alpha2/3 and numeric codes
  name: string;
  alpha2: CountryAlpha2; // 'US' | 'NZ' ...
  alpha3: CountryAlpha3; // 'USA' | 'NZL' ...
  numeric: CountryNumeric; // '840' | '554' ...

  // ISO 4217 currencies
  currencyCodes: CurrencyAlphabeticCode[];
}

export interface Currency {
  name: CurrencyName; // 'US Dollar' | 'New Zealand Dollar' ...
  alphabeticCode: CurrencyAlphabeticCode; // 'USD' | 'NZD' ...
  numericCode: CurrencyNumericCode; // '840' | '554' ...
  minorUnits?: number;
  isFund?: true;
}

Functions

Formatting

  • format({ amount, currency }: Money, options?: CurrencyFormatOptions, locales?: string | string[]): string

Parsing

  • parse(money: string, currency: CurrencyAlphabeticCode, locales?: string | string[]) => Money;

Currencies

  • allCurrencies(): Currency[]
  • findCurrency(search: string): Currency
  • getCurrency(code: CurrencyAlphabeticCode | CurrencyNumericCode): Currency
  • getMinorUnitDigits(currency: CurrencyAlphabeticCode)
  • getSymbol(currency: CurrencyAlphabeticCode, locales?: string | string[]): string

Countries

  • allCountries(): Country[]
  • getCountry(code: CountryAlpha2 | CountryAlpha3 | CountryNumeric): Country
  • getCountriesForCurrency(code: CurrencyAlphabeticCode): CountryAlpha3[]

Usage examples

format

currency('2023-11-02T15:35:47.191Z').format({
  amount: '123456789012345678901234567890',
  currency: 'USD',
});
// $1,234,567,890,123,456,789,012,345,678.90

currency('2023-11-02T15:35:47.191Z').format({
  amount: '123456',
  currency: 'USD',
});
// $1234.56

currency('2023-11-02T15:35:47.191Z').format(
  { amount: '123456', currency: 'USD' },
  {
    useGrouping: false,
    useCurrency: false,
  },
);
// 1234.56

currency('2023-11-02T15:35:47.191Z').format(
  { amount: '123456', currency: 'USD' },
  {
    useGrouping: false,
    useCurrency: false,
    useDecimal: false,
  },
);
// 123456

currency('2023-11-02T15:35:47.191Z').format({ amount: '-0', currency: 'USD' });
// -$0.00

currency('2023-11-02T15:35:47.191Z').format(
  { amount: '123456789', currency: 'USD' },
  {
    currencyDisplay: 'code',
  },
  'de-DE',
);
// 1.234.567,89 USD

currency('2023-11-02T15:35:47.191Z').format(
  { amount: '123456', currency: 'USD' },
  {
    useDecimal: false,
    useGrouping: false,
    useCurrency: false,
  },
  'as-IN',
);
// ১২৩৪৫৬

parse

currency('2023-11-02T15:35:47.191Z').parse('.1', 'USD');
// { amount: '10', currency: 'USD' }

currency('2023-11-02T15:35:47.191Z').parse('1', 'USD');
// { amount: '100', currency: 'USD' }

currency('2023-11-02T15:35:47.191Z').parse('$1.23', 'USD');
// { amount: '123', currency: 'USD' }

currency('2023-11-02T15:35:47.191Z').parse('123.456,78', 'EUR', 'de-DE');
// { amount: '12345678', currency: 'EUR' }

currency('2023-11-02T15:35:47.191Z').parse('10€', 'EUR');
// { amount: '1000', currency: 'EUR' }

currency('2023-11-02T15:35:47.191Z').parse('10', 'JPY');
// { amount: '10', currency: 'JPY' }

getSymbol

currency('2023-11-02T15:35:47.191Z').getSymbol('USD');
// $

currency('2023-11-02T15:35:47.191Z').getSymbol('NZD');
// NZ$

currency('2023-11-02T15:35:47.191Z').getSymbol('NZD', 'en-NZ');
// $

getMinorUnitDigits

currency('2023-11-02T15:35:47.191Z').getMinorUnitDigits('USD');
// 2

currency('2023-11-02T15:35:47.191Z').getMinorUnitDigits('JPY');
// 0

findCurrency

currency('2023-11-02T15:35:47.191Z').findCurrency('us dollar');
// {
//   name: 'US Dollar',
//   alphabeticCode: 'USD',
//   numericCode: '840',
//   minorUnits: 2
// }

currency('2023-11-02T15:35:47.191Z').findCurrency(554);
// {
//   name: 'New Zealand Dollar',
//   alphabeticCode: 'NZD',
//   numericCode: '554',
//   minorUnits: 2
// }

getCurrency

currency('2023-11-02T15:35:47.191Z').getCurrency('840');
// {
//   name: 'US Dollar',
//   alphabeticCode: 'USD',
//   numericCode: '840',
//   minorUnits: 2
// }

currency('2023-11-02T15:35:47.191Z').getCurrency('NZD');
// {
//   name: 'New Zealand Dollar',
//   alphabeticCode: 'NZD',
//   numericCode: '554',
//   minorUnits: 2
// }

getCountry

currency('2023-11-02T15:35:47.191Z').getCountry('USA');
// {
//   name: 'US',
//   alpha2: 'US',
//   alpha3: 'USA',
//   numeric: '840',
//   currencyCodes: [ 'USD' ]
// }

getCountriesForCurrency

currency('2023-11-02T15:35:47.191Z').getCountriesForCurrency('NZD');
// [ 'COK', 'NIU', 'NZL', 'PCN', 'TKL' ]

License

MIT

版本与发布记录

@checkdigit/currency 共发布 93 个版本,首次发布于 2020/03/10;过去 12 个月发布了 33 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
9.0.0latest2026/09/092 个月前
9.0.0-PR.32-fd29beta2026/09/082 个月前
9.0.0-PR.32-b11c2026/09/052 个月前
9.0.0-PR.32-87cd2026/09/052 个月前
9.0.0-PR.32-c6602026/09/052 个月前
8.0.02025/11/2111 个月前

关于 @checkdigit/currency 的常见问题

@checkdigit/currency 是什么?

Standard currency handling

如何安装 @checkdigit/currency?

在项目目录运行 npm install @checkdigit/currency,或使用 pnpm add / yarn add / bun add。

@checkdigit/currency 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@checkdigit/currency 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@checkdigit/currency 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24.18.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@checkdigit/currency 可以商用吗?

@checkdigit/currency 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@checkdigit/currency 的下载量有多少?

根据 npm 官方下载统计,@checkdigit/currency 在 2026/10/10 – 2026/11/08 这 30 天内被下载 2,752 次,日均约 92 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@checkdigit/currency 还在维护吗?

最新版本 9.0.0 发布于 2026/09/09(2 个月前);至今共发布 93 个版本,其中过去 12 个月发布了 33 个;首次发布于 2020/03/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。