accounting-js vs numeral
Formatting Currency and Numbers in JavaScript
accounting-jsnumeral类似的npm包:

Formatting Currency and Numbers in JavaScript

accounting-js and numeral are legacy JavaScript libraries designed to format numbers, currency, and percentages for display in web applications. accounting-js focuses specifically on financial data, offering robust money formatting with configurable symbols and precision. numeral provides a broader range of formatting options, including bytes, time, and ordinal numbers, using a chaining API. Both libraries were popular in the early 2010s but are now considered stagnant compared to modern native solutions.

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
accounting-js011946.9 kB121 年前MIT
numeral09,697-34410 年前MIT

Accounting-js vs Numeral: Legacy Number Formatting Compared

Both accounting-js and numeral were created to solve the same problem — JavaScript lacks built-in number formatting tools that are easy to use. Before Intl.NumberFormat became widely supported, developers relied on these libraries to display money and statistics. Today, both packages are considered legacy software. They are no longer actively maintained, and security updates are rare. Let's compare how they work and why you should think twice before using them.

⚠️ Maintenance and Future Proofing

accounting-js has not seen significant updates in many years. The repository is mostly inactive, and issues often go unanswered. Using it in a new project introduces technical debt immediately.

// accounting-js: Last stable version is 0.4.1
// No recent patches for security or modern JS features
import accounting from 'accounting-js';

numeral is in a similar state. The original repository is archived or inactive, and the npm package has not been updated since around 2016. Community forks exist, but they fragment the ecosystem.

// numeral: Last stable version is 2.0.6
// Repository often marked as archived or unmaintained
import numeral from 'numeral';

💡 Warning: For any new project, use the native Intl.NumberFormat API instead. It is built into the browser, supports all locales, and receives regular updates from engine maintainers.

🛠️ API Style: Global Functions vs Chaining

accounting-js uses a simple global object with static methods. You pass the value and options directly to the function. This style is straightforward but less flexible for complex pipelines.

// accounting-js: Direct function calls
import accounting from 'accounting-js';

const price = accounting.formatMoney(1234.56, '$', 2);
// Output: "$1,234.56"

const clean = accounting.unformat("$1,234.56");
// Output: 1234.56

numeral uses a chaining pattern. You wrap a value, call format methods, and then retrieve the result. This allows for more readable sequences when transforming data.

// numeral: Chaining API
import numeral from 'numeral';

const price = numeral(1234.56).format('$0,0.00');
// Output: "$1,234.56"

const clean = numeral("$1,234.56").value();
// Output: 1234.56

💰 Money Formatting Capabilities

accounting-js is specialized for financial data. It handles negative numbers with parentheses by default, which is standard in accounting. You can configure symbols and precision easily.

// accounting-js: Built-in accounting standards
import accounting from 'accounting-js';

// Negative values show in parentheses
const loss = accounting.formatMoney(-500, '$', 2);
// Output: "($500.00)"

// Custom symbol and separator
const euro = accounting.formatMoney(1000, '€', 2, '.', ',');
// Output: "€1.000,00"

numeral can format money, but you must define the pattern manually. It does not enforce accounting standards like parentheses for negatives unless you build that into the format string.

// numeral: Manual pattern definition
import numeral from 'numeral';

// Negative values show with a minus sign by default
const loss = numeral(-500).format('$0,0.00');
// Output: "-$500.00"

// Custom pattern for European style
const euro = numeral(1000).format('0.000,00 $');
// Output: "1.000,00 $"

🌍 Localization and Number Types

accounting-js focuses almost entirely on currency. It does not handle bytes, percentages, or time durations well. Localization is limited to manually setting separators.

// accounting-js: Limited to numbers and money
import accounting from 'accounting-js';

// No built-in support for bytes or percentages
// You must calculate percentages manually before formatting
const percent = accounting.formatNumber(0.5 * 100) + '%';
// Output: "50%"

numeral supports many data types beyond money. You can format bytes, durations, and ordinal numbers (1st, 2nd, 3rd) out of the box. This made it popular for dashboards.

// numeral: Supports multiple data types
import numeral from 'numeral';

// Bytes
const size = numeral(1024).format('0.0 b');
// Output: "1.0 KB"

// Percentages
const rate = numeral(0.5).format('0%');
// Output: "50%"

// Ordinals
const rank = numeral(1).format('0o');
// Output: "1st"

🔄 The Modern Standard: Intl.NumberFormat

Neither library is recommended for modern development. The native Intl.NumberFormat API covers most use cases with better performance and accuracy. It handles localization automatically based on the user's browser settings.

// Native API: Recommended for all new projects
const formatter = new Intl.NumberFormat('en-US', {
  style: 'currency',
  currency: 'USD'
});

const price = formatter.format(1234.56);
// Output: "$1,234.56"

// Handles negatives standardly
const loss = formatter.format(-500);
// Output: "-$500.00"

📊 Summary: Key Differences

Featureaccounting-jsnumeral
Primary Focus💰 Financial money formatting📊 General number formatting
API Style🛠️ Static functions🔗 Chaining methods
Negative Numbers📉 Parentheses ($100)➖ Minus sign -$100
Data Types🔢 Numbers and Currency📏 Bytes, Time, Percent, Ordinal
Maintenance❌ Inactive❌ Inactive

💡 Final Recommendation

accounting-js is a specialized tool for older financial apps that need specific accounting rules like parentheses for negatives. If you are maintaining such a system, it might still work. However, do not start a new project with it.

numeral offers more variety for dashboards that need to show bytes, time, or percentages alongside money. It is more flexible but equally outdated.

Final Thought: Both libraries served a purpose when browsers lacked native tools. Today, Intl.NumberFormat is the standard. It is faster, smaller, and maintained by browser vendors. Only use accounting-js or numeral if you are forced to support very old browsers or maintain legacy code that cannot be refactored.

如何选择: accounting-js vs numeral

  • accounting-js:

    Choose accounting-js if you are maintaining an older codebase that relies heavily on specific financial formatting rules, such as custom currency symbols and decimal precision for money. It is suitable for legacy projects where changing to a new library would introduce too much risk. Do not use this for new projects due to lack of maintenance.

  • numeral:

    Choose numeral if you need to format diverse data types like bytes, percentages, and time durations in addition to currency, and you are stuck on an older stack. Its chaining API allows for flexible output patterns. However, avoid using this in new architectures because it is no longer actively updated and lacks modern localization features.

accounting-js的README

NPM version

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.

Documentation

Quickstart

Install

npm install accounting-js

Use

Format number

import { formatNumber } from 'accounting-js';

// Default usage
formatNumber(5318008);
// ⇨ 5,318,008

// Custom format
formatNumber(9876543.21, { precision: 3, thousand: " " });
// ⇨ 9 876 543.210

Format money

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

Convert money to numeric

import { unformat } from 'accounting-js';

unformat('£ 12,345,678.90 GBP');
// ⇨ 12345678.9

Accounting toFixed()

// 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