accounting.js

A lightweight, dependency-free JavaScript library for parsing and formatting numbers, money, and currency values.

Library
npm
v0.4.1
4,980stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
45/100Fair
Development Activity0
Maintenance0
Community80
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
53/100Fair
Architecture60
Code Quality40
Innovation48
Learning Curve65

accounting.js is a small utility library that solves the recurring problem of formatting numbers as money, currency amounts, or grouped numbers in JavaScript. It exposes a handful of pure functions - formatNumber, formatMoney, formatColumn, unformat, and toFixed - that convert between raw numeric values and human-readable, locale-configurable strings without pulling in a full internationalization framework.

The library has no runtime dependencies, works identically in the browser and in Node.js (CommonJS and AMD module patterns are both supported), and its behavior is controlled entirely through a plain settings object for currency symbol, decimal separator, thousands separator, and precision. It also works around floating-point rounding quirks in toFixed that are common headaches when handling money values.

What You Get

  • formatMoney() for currency-formatted strings with configurable symbol, precision, and separators
  • formatNumber() for grouped, comma-separated number formatting independent of currency
  • unformat()/parse() to strip formatting and recover the raw numeric value, including bracketed negatives
  • formatColumn() to align a list of currency values into a whitespace-padded column
  • A toFixed() replacement that avoids common binary floating-point rounding errors

Common Use Cases

  • Displaying prices and totals in an e-commerce or invoicing UI
  • Formatting financial report figures into aligned currency columns
  • Parsing user-entered currency strings back into numeric values for calculations
  • Localising number and currency display for different regions without a full i18n library

Under The Hood

Architecture accounting.js is a single-file IIFE module with no internal layering: a closure exposes a lib object holding a settings config and a small set of internal helpers (isString, isArray, isObject, defaults, map, checkPrecision, checkCurrencyFormat) that the public API methods (unformat, toFixed, formatNumber, formatMoney, formatColumn) build on. There is no dependency injection or external state - all configuration flows through the shared lib.settings object or per-call option objects, and the module-definition block at the end branches on exports/module.exports/AMD define/global root to support CommonJS, AMD, and plain browser script usage from the same source file.

Tech Stack The library is plain ES5 JavaScript with zero runtime dependencies (package.json declares an empty dependency list) and no build step - the distributed accounting.js and accounting.min.js files are committed directly to the repo rather than generated by a bundler in CI. It ships via npm and Bower with matching manifests, and is consumed as a Node/CommonJS module, an AMD module, or a global window.accounting object.

Code Quality Test coverage exists as bundled Jasmine and QUnit suites (tests/jasmine, tests/qunit) run through static HTML runners rather than an automated npm test/CI pipeline - no CI configuration is present in the repo. Error handling is deliberately permissive: comments in the source note that invalid input “fails silently” and coerces to 0 rather than throwing, which is a documented but non-strict design choice. There is no linter config, no TypeScript types, and no type checking; naming is consistent camelCase with JSDoc-style comments on every public method.

What Makes It Unique The standout technical detail is its toFixed() reimplementation, which routes through exponential notation (Number(value + 'e' + precision)) specifically to avoid native toFixed’s binary floating-point rounding bugs - a real, well-known problem for money math in JavaScript. formatColumn() is also a distinguishing feature versus most similar formatters: it right-aligns a list of formatted values by padding with whitespace, useful for monospaced financial reporting output. Otherwise its formatting approach is standard for the era; modern codebases increasingly rely on the native Intl.NumberFormat API for the same job.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search