accounting vs currency.js vs format-number vs intl vs numbro vs numeral
JavaScript 数字格式化与货币处理库深度对比
accountingcurrency.jsformat-numberintlnumbronumeral类似的npm包:

JavaScript 数字格式化与货币处理库深度对比

accountingcurrency.jsformat-numberintlnumbronumeral 都是用于处理数字格式化、货币显示及数学计算的 JavaScript 工具库。它们旨在解决前端开发中常见的数字展示问题,例如千分位分隔、货币符号添加、精度控制以及本地化格式支持。其中 numeral 是早期的流行方案,numbro 是其活跃的分叉版本,currency.js 专注于不可变的货币对象计算,而 intl 包主要用于为旧浏览器提供原生 Intl API 的 polyfill。在现代开发中,选择哪个库取决于项目对维护性、功能复杂度以及浏览器兼容性的具体要求。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
accounting04,979-14512 年前-
currency.js03,382-615 年前MIT
format-number050-69 年前BSD-3-Clause
intl01,691-7910 年前MIT
numbro01,1421.27 MB2692 年前MIT
numeral09,706-3459 年前MIT

6 大数字格式化库:架构选型与实战对比

在前端开发中,处理数字和货币看似简单,实则陷阱重重。千分位分隔、货币符号位置、小数精度以及本地化差异,任何一个细节处理不当都可能导致用户体验下降甚至财务数据错误。accountingcurrency.jsformat-numberintlnumbronumeral 是生态系统中常见的解决方案,但它们的现状和能力差异巨大。本文将从维护状态、核心功能代码实现以及现代替代方案三个维度进行深度剖析。

🚨 维护状态与风险评估

架构选型的第一步是确认库是否活着。使用已停止维护的库会给项目带来安全隐患和技术债务。

  • numeral:已停止维护。最后更新停留在多年前,社区已转向其分叉版本。
  • accounting:遗留库。功能单一,多年无重大更新,仅适合旧项目维护。
  • intl:Polyfill 方案。随着浏览器对原生 Intl 支持的普及,其必要性正在降低。
  • numbro:活跃维护。作为 numeral 的继任者,修复了许多 bug 并保持了 API 兼容。
  • currency.js:活跃维护。专注于货币计算,轻量且可靠。
  • format-number:稳定但简单。功能有限,适合特定轻量场景。

💡 建议:除非有强制的兼容性需求,否则避免在新项目中引入 numeralaccounting

💰 货币格式化能力对比

货币格式化不仅是在数字前加个符号,还涉及千分位、小数位精度以及符号位置(如 ¥100 vs 100¥)。以下是各库实现相同需求的代码对比。

accounting 提供专门的货币格式化方法,配置项较为繁琐。

import accounting from 'accounting';

// 格式化为货币
accounting.formatMoney(1234567.89, "$", 2, 

如何选择: accounting vs currency.js vs format-number vs intl vs numbro vs numeral

  • accounting:

    仅建议在维护遗留系统时使用,该库已多年未更新,缺乏现代 JavaScript 特性支持。如果项目不需要复杂的链式调用且只需简单的货币格式化,它可以勉强胜任,但在新项目中应避免引入。

  • currency.js:

    选择 currency.js 如果你需要处理货币计算且关心精度问题。它提供了不可变的货币对象,适合电商或金融场景中需要频繁进行加减运算并确保小数位准确的场景。

  • format-number:

    选择 format-number 当你只需要极简单的数字字符串格式化,而不需要货币符号或本地化支持。它非常轻量,适合对 bundle 大小敏感且逻辑简单的工具类项目。

  • intl:

    选择 intl 包仅当你必须支持不支持原生 Intl API 的旧浏览器(如 IE)。对于现代项目,直接使用浏览器原生的 Intl 对象是更好的选择,无需额外依赖。

  • numbro:

    选择 numbro 如果你需要丰富的格式化选项且希望基于 numeral 的语法但获得更好的维护支持。它是 numeral 的活跃分叉版本,适合需要复杂数字格式化但不想迁移到原生 API 的项目。

  • numeral:

    不应在新项目中使用 numeral,该库已停止维护多年,存在未修复的 bug 且不支持现代构建工具。如果现有项目正在使用它,建议制定计划迁移到 numbro 或原生 Intl API。

accounting的README

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.


Works great with:

  • money.js - a tiny (1kb) standalone JavaScript currency conversion library, for web & nodeJS
  • Open Exchange Rates - the free currency conversion data API

Changelog

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

  • Rewrote library structure similar to underscore.js for use as a nodeJS/npm and AMD module. Use npm install accounting and then var accounting = require("accounting"); in your nodeJS scripts.
  • Also works with requireJS or any AMD module loader.
  • unformat now only attempts to parse the number if it's not already a valid number.
  • acounting.unformat now also aliased as acounting.parse
  • Fixed an IE bug in the defaults method

v0.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

  • Rewrote formatting system for formatMoney and formatColumn for better control of string output
  • Now supports separate formats for negative and zero values (optionally) via accounting.settings.currency.format
  • Internal improvements and helper methods

v0.1.4

  • formatMoney recursively formats arrays
  • Added Jasmine test suite (thanks to millermedeiros) and QUnit functionality/speed tests

v0.1.3

  • Added configurable settings object for default formatting parameters.
  • Added format parameter to control symbol and value position (default "%s%v", or [symbol][value])
  • Methods consistently accept object as 2nd parameter, matching/overriding the library defaults

v0.1.2

  • formatColumn works recursively on nested arrays (e.g. accounting.formatColumn( [[1,12,123,1234], [1234,123,12,1]] ), returns matching array with inner columns lined up)
  • Fix rounding in formatNumber

v0.1.1

  • Added toFixed method (accounting.toFixed(value, precision)), which treats floats more like decimals for more accurate currency rounding
  • Minified version preserves semicolons
  • Fixed NaN errors when no value in unformat

v0.1 - First version