
处理全球化的项目时,管理国家和货币列表及其格式化方式可能很复杂。 country-currency-utils npm 包应运而生,它以 TypeScript 编写,旨在简化这个过程,无论是在前端还是后端。该包避免在代码库中直接包含庞大的国家/地区和货币数据,而是通过 CDN 获取这些数据,从而保持代码简洁。
国家数据
国家数据通过 countries_details_url 变量访问,返回一个键值对对象,键为双字母 ISO 国家代码,值为包含国家名称、拨号代码、货币代码和国旗表情符号的对象。 包中提供以下函数:
type tcountrydetails = {
name: string;
dialcode: string;
currencycode: string;
flagemoji: string;
};
type tcountrydata = tcountrydetails & {
countrycode: string;
};
getallcountrydetails(): Promise>;
getallcountrydata(): Promise;
getcountrydata(countrycode: string): Promise;
getcountriesdata(countrycodes: string[]): Promise;
这些函数允许轻松检索单个国家或多个国家的数据。
货币数据
类似地,货币数据通过 currencies_details_url 变量访问。 它包含一个键值对对象,键为 ISO 货币代码,值为包含货币名称、符号(本地和标准)、小数位数、数字分组等详细信息的对象。 相关的函数如下:
type tcurrencydetails = {
name: string;
demonym: string;
majorsingle: string;
majorplural: string;
symbol: string;
symbolnative: string;
symbolpreferred: string;
minorsingle: string;
minorplural: string;
decimals: number;
decimalscompact: number;
digitgrouping: 2 | 3;
};
type tcurrencydata = tcurrencydetails & {
currencycode: string;
};
getallcurrencydetails(): Promise>;
getallcurrencydata(): Promise;
getcurrencydata(currencycode: string): Promise;
getcurrenciesdata(currencycodes: string[]): Promise;
这些函数允许获取单个或多个货币的详细信息。
金额格式化工具
BJXShop网上购物系统是一个高效、稳定、安全的电子商店销售平台,经过近三年市场的考验,在中国网购系统中属领先水平;完善的订单管理、销售统计系统;网站模版可DIY、亦可导入导出;会员、商品种类和价格均实现无限等级;管理员权限可细分;整合了多种在线支付接口;强有力搜索引擎支持... 程序更新:此版本是伴江行官方商业版程序,已经终止销售,现于免费给大家使用。比其以前的免费版功能增加了:1,整合了论坛
该包提供了强大的金额格式化工具,处理小数位数、货币符号和数字分组:
-
四舍五入:
getroundedamount和getroundedamountoncurrency函数分别根据指定小数位数或货币规则对金额进行四舍五入。 -
金额格式化:
getformattedamountoncurrency函数根据货币规则格式化金额,包括添加逗号分隔符。 -
金额显示:
getdisplayamountoncurrency和getDisplayAmountOnCurrencyCode函数将格式化的金额与货币符号组合,并提供选项来控制符号类型(标准或本地)和分隔符。
总结
country-currency-utils 提供了一种高效且易于使用的方法来管理国家和货币数据,并格式化货币金额,从而简化国际化项目的开发。 如果您需要处理多种货币和国家/地区,这个包值得一试。 请访问 GitHub 仓库了解更多信息。









