accounting、currency.js、format-number、intl、numbro 和 numeral 都是用于处理数字格式化、货币显示及数学计算的 JavaScript 工具库。它们旨在解决前端开发中常见的数字展示问题,例如千分位分隔、货币符号添加、精度控制以及本地化格式支持。其中 numeral 是早期的流行方案,numbro 是其活跃的分叉版本,currency.js 专注于不可变的货币对象计算,而 intl 包主要用于为旧浏览器提供原生 Intl API 的 polyfill。在现代开发中,选择哪个库取决于项目对维护性、功能复杂度以及浏览器兼容性的具体要求。
在前端开发中,处理数字和货币看似简单,实则陷阱重重。千分位分隔、货币符号位置、小数精度以及本地化差异,任何一个细节处理不当都可能导致用户体验下降甚至财务数据错误。accounting、currency.js、format-number、intl、numbro 和 numeral 是生态系统中常见的解决方案,但它们的现状和能力差异巨大。本文将从维护状态、核心功能代码实现以及现代替代方案三个维度进行深度剖析。
架构选型的第一步是确认库是否活着。使用已停止维护的库会给项目带来安全隐患和技术债务。
numeral:已停止维护。最后更新停留在多年前,社区已转向其分叉版本。accounting:遗留库。功能单一,多年无重大更新,仅适合旧项目维护。intl:Polyfill 方案。随着浏览器对原生 Intl 支持的普及,其必要性正在降低。numbro:活跃维护。作为 numeral 的继任者,修复了许多 bug 并保持了 API 兼容。currency.js:活跃维护。专注于货币计算,轻量且可靠。format-number:稳定但简单。功能有限,适合特定轻量场景。💡 建议:除非有强制的兼容性需求,否则避免在新项目中引入
numeral和accounting。
货币格式化不仅是在数字前加个符号,还涉及千分位、小数位精度以及符号位置(如 ¥100 vs 100¥)。以下是各库实现相同需求的代码对比。
accounting 提供专门的货币格式化方法,配置项较为繁琐。
import accounting from 'accounting';
// 格式化为货币
accounting.formatMoney(1234567.89, "$", 2,
仅建议在维护遗留系统时使用,该库已多年未更新,缺乏现代 JavaScript 特性支持。如果项目不需要复杂的链式调用且只需简单的货币格式化,它可以勉强胜任,但在新项目中应避免引入。
选择 currency.js 如果你需要处理货币计算且关心精度问题。它提供了不可变的货币对象,适合电商或金融场景中需要频繁进行加减运算并确保小数位准确的场景。
选择 format-number 当你只需要极简单的数字字符串格式化,而不需要货币符号或本地化支持。它非常轻量,适合对 bundle 大小敏感且逻辑简单的工具类项目。
选择 intl 包仅当你必须支持不支持原生 Intl API 的旧浏览器(如 IE)。对于现代项目,直接使用浏览器原生的 Intl 对象是更好的选择,无需额外依赖。
选择 numbro 如果你需要丰富的格式化选项且希望基于 numeral 的语法但获得更好的维护支持。它是 numeral 的活跃分叉版本,适合需要复杂数字格式化但不想迁移到原生 API 的项目。
不应在新项目中使用 numeral,该库已停止维护多年,存在未修复的 bug 且不支持现代构建工具。如果现有项目正在使用它,建议制定计划迁移到 numbro 或原生 Intl API。
accounting.js is a tiny JavaScript library for number, money and currency parsing/formatting. It's lightweight, fully localisable, has no dependencies, and works great client-side or server-side. Use standalone or as a nodeJS/npm and AMD/requireJS module.
Visit the plugin homepage for demos and documentation: http://openexchangerates.github.io/accounting.js/
Please checkout or download the latest stable tag before using in production. Bug reports and pull requests are welcome.
Maintained by Open Exchange Rates and originally by @josscrowcroft and other contributors.
v0.4.1 - Alias accounting.formatNumber() as accounting.format()
v0.4 - Transferred repository to Open Exchange Rates for ongoing maintenance
v0.3.2 - Fixed package.json dependencies (should be empty object)
v0.3.0
npm install accounting and then var accounting = require("accounting"); in your nodeJS scripts.acounting.unformat now also aliased as acounting.parsedefaults methodv0.2.2 - Fixed same issue as #Num: #24 in formatNumber; switch to Google Closure Compiler for minified version.
v0.2.1 - Fixed issue #Num: #24 (locally-defined settings object was being modified by formatMoney)
v0.2
accounting.settings.currency.formatv0.1.4
v0.1.3
format parameter to control symbol and value position (default "%s%v", or [symbol][value])v0.1.2
accounting.formatColumn( [[1,12,123,1234], [1234,123,12,1]] ), returns matching array with inner columns lined up)v0.1.1
accounting.toFixed(value, precision)), which treats floats more like decimals for more accurate currency roundingNaN errors when no value in unformatv0.1 - First version