accounting-js 和 numeral 都是用于格式化数字的 JavaScript 库,但侧重点不同。accounting-js 专为财务场景设计,提供严格的货币格式化功能,处理符号和精度更简单。numeral 则更通用,支持百分比、字节、时间等多种格式,灵活性更高。在现代开发中,两者都面临原生 Intl API 的竞争,但在特定遗留系统或特殊需求下仍有价值。
在 JavaScript 项目中处理数字展示时,accounting-js 和 numeral 是两个经典的选择。它们都能将原始数字转换为用户友好的字符串,但设计目标和适用场景有明显区别。让我们从实际工程角度对比它们的用法和限制。
accounting-js 专为金钱设计,API 直接暴露货币符号、精度和分隔符参数。
// accounting-js: 直接配置货币参数
import accounting from 'accounting-js';
accounting.formatMoney(1234567.89, "$", 2, ",", ".");
// 输出:$1,234,567.89
accounting.formatMoney(-1234.50, "¥", 2);
// 输出:¥(1,234.50) - 默认负数格式
numeral 使用格式字符串模板,更灵活但需要手动定义货币符号。
// numeral: 使用格式字符串
import numeral from 'numeral';
numeral(1234567.89).format('$0,0.00');
// 输出:$1,234,567.89
numeral(-1234.50).format('$0,0.00');
// 输出:-$1,234.50 - 需手动处理负数样式
accounting-js 仅提供基础的数字格式化,功能单一。
// accounting-js: 仅支持基础数字
import accounting from 'accounting-js';
accounting.formatNumber(10000, 2, ",", ".");
// 输出:10,000.00
numeral 支持多种数据类型格式化,适合复杂场景。
// numeral: 支持多种格式
import numeral from 'numeral';
numeral(0.75).format('0%'); // 输出:75%
numeral(1024).format('0b'); // 输出:1KB
numeral(10000).format('0,0'); // 输出:10,000
accounting-js 维护相对稳定,但功能更新缓慢。
// accounting-js: 手动配置本地化
accounting.formatMoney(1000, "€", 2, ".", ",");
// 欧洲格式:€1.000,00
numeral 社区普遍认为已进入维护模式,更新极少。
// numeral: 加载语言包(需额外配置)
numeral.locale('de');
numeral(1000).format('0,0');
// 输出:1.000 (取决于语言包支持)
尽管侧重点不同,这两个库在基础功能上有重叠。
// 两者都支持 CommonJS 和浏览器全局变量
// accounting.js: window.accounting
// numeral: window.numeral
// accounting-js: 反格式化
accounting.unformat("$1,000.00");
// 输出:1000
// numeral: 反格式化
numeral("$1,000.00").value();
// 输出:1000
Intl API 自动识别 locale 方便。// 两者都需手动指定分隔符
// accounting: formatNumber(val, precision, thousand, decimal)
// numeral: 依赖格式字符串如 '0,0.00'
| 特性 | accounting-js | numeral |
|---|---|---|
| 核心定位 | 💰 专为货币设计 | 🔢 通用数字格式化 |
| API 风格 | 函数参数配置 | 链式调用 + 格式字符串 |
| 支持格式 | 货币、基础数字 | 货币、百分比、字节、时间等 |
| 负数处理 | 默认括号样式 (财务标准) | 默认负号 (需手动配置) |
| 维护状态 | 稳定但更新少 | 社区视为遗留/维护模式 |
| 本地化 | 手动配置符号 | 手动配置或加载语言包 |
accounting-js 就像一把专用的财务尺子 📏——适合对货币显示有严格要求的场景,如电商价格、金融报表。它的默认行为更符合会计习惯,减少配置错误。
numeral 就像一把多功能瑞士军刀 🔪——适合需要格式化多种数据类型(百分比、字节等)的旧项目。但在新项目中,建议优先考虑原生 Intl.NumberFormat 以获得更好的本地化支持和长期维护保障。
核心思考:如果项目必须依赖第三方库,且主要是货币展示,选 accounting-js 更稳妥。如果需要通用格式化且无法使用原生 API,numeral 可用但需意识到维护风险。现代开发中,原生 Intl 通常是更优解。
选择 accounting-js 如果你的核心需求是货币展示,需要简单的 API 来处理符号、精度和千位分隔符,且不希望引入复杂的配置。适合电商、金融仪表盘等对金额显示要求严格的场景,它的默认行为更符合财务习惯。
选择 numeral 如果你需要格式化非货币数字(如百分比、字节大小),且项目无法使用原生 Intl API。但需注意其维护状态,社区普遍认为它已进入维护模式,适合维护旧项目而非新架构。
accounting-js is a tiny JavaScript library for number, money and currency parsing/formatting. It's lightweight, fully localizable, has no dependencies, and works great client-side or server-side. Use standalone or as a nodeJS/npm and AMD/requireJS module.
npm install accounting-js
import { formatNumber } from 'accounting-js';
// Default usage
formatNumber(5318008);
// ⇨ 5,318,008
// Custom format
formatNumber(9876543.21, { precision: 3, thousand: " " });
// ⇨ 9 876 543.210
import { formatMoney } from 'accounting-js';
// Default usage
formatMoney(12345678);
// ⇨ $12,345,678.00
// European formatting (custom symbol and separators)
formatMoney(4999.99, { symbol: "€", precision: 2, thousand: ".", decimal: "," });
// ⇨ €4.999,99
import { unformat } from 'accounting-js';
unformat('£ 12,345,678.90 GBP');
// ⇨ 12345678.9
// Native toFixed has rounding issues
(0.615).toFixed(2);
// ⇨ '0.61'
// With accounting-js
toFixed(0.615, 2);
// ⇨ '0.62'
Copyright (c) 2016-present Stanislav Lesnikov, MIT License
Copyright (c) 2014 Open Exchange Rates, MIT License