Skip to main content
← Guides

Guide

Dinero.js vs currency.js vs Intl.NumberFormat

Compare Dinero.js, currency.js and Intl.NumberFormat for money arithmetic, allocation, storage and display. See tested examples, precision limits and tradeoffs.

Updated PkgPulse Team9 min read
0
Contents

TL;DR

Use Dinero.js for currency-aware monetary values and allocations, currency.js for a small fixed-precision arithmetic API, and Intl.NumberFormat to format a value for display. These tools solve different parts of the problem. Define the amount, currency and scale before calculating, preserve them when storing data, and test rounding and allocation rules explicitly. No library choice establishes regulatory compliance.

The decision matrix combines the Dinero representation, currency.js API and Intl.NumberFormat specification. Fits are editorial recommendations, not an overall ranking.

Decision matrix

ToolRepresentation and arithmeticSplitting a totalDisplayEvaluate it when
Dinero.js 2.0.2Amount, currency object and scale; number or bigint interfacesallocate takes ratios and distributes remainder unitstoDecimal, then a separate locale formatterCurrency identity and scale belong in each value
currency.js 2.0.4Integer-backed arithmetic with configurable precision and a bounded number rangedistribute divides a total among entriesformat with explicit options, or a separate formatterYour application owns currency identity and uses fixed precision
Intl.NumberFormatFormats the numeric value supplied by the callerNo allocation operationLocale- and currency-sensitive outputYou already have a calculated amount to display

Sources: Dinero quick start, allocation, large-number interfaces, currency.js, ECMA-402. Package baselines come from the Dinero and currency.js registry records.

Choose the amount, currency and scale

Start by writing what an amount means. Is the stored integer a count of minor units, or does it carry extra precision for intermediate calculations? Which currency does it represent? Where does the application reduce precision? Make these answers part of the data contract before choosing an arithmetic API.

Dinero separates a currency's exponent from a value's scale. The default scale follows the exponent, but a value can use a higher scale. Its documentation illustrates this with amounts that need precision beyond payable minor units. Dropping the scale during storage can therefore change the meaning of the amount. See the scale guide.

For the USD examples below, default scale is 2. That does not make cents the universal representation. Keep the interpretation alongside the value, especially when reading another service's data. Dinero's currency definitions include code, base and exponent; the snapshot API includes scale separately.

Dinero.js: release API and allocations

The examples target dinero.js 2.0.2. Its published manifest requires Node >=20 and exposes currencies through dinero.js/currencies. The quick-start page contains an older Node requirement, so use the published package as the runtime requirement for this release. The quick start supplies the functional API pattern.

import { dinero, add, allocate, toDecimal, toSnapshot } from 'dinero.js';
import { USD } from 'dinero.js/currencies';

const total = add(
  dinero({ amount: 1999, currency: USD }),
  dinero({ amount: 160, currency: USD })
);

toDecimal(total); // '21.59'
toSnapshot(total).scale; // 2

const shares = allocate(
  dinero({ amount: 100, currency: USD }),
  [1, 1, 1]
);
shares.map(value => toSnapshot(value).amount); // [34, 33, 33]

The addition combines two explicit USD amounts. It does not calculate a tax rate or select a rounding policy. The allocation starts with 100 minor units and returns 34, 33 and 33. Their sum remains 100. This is the documented purpose of allocate: split by ratios and distribute the remainder, rather than round each share independently.

Use allocation when the shares must conserve a total. Keep display rounding as a separate decision. A formatted string is not an allocation result, and changing displayed fraction digits does not define who receives remainder units. The allocation API and formatting guidance cover those separate operations.

Dinero also supplies convert. The caller supplies the target currency and rates; it is not a rate-fetching service. Fractional rates use scaled amounts, such as an amount of 89 with scale 2, rather than an unqualified floating-point multiplier. Follow the conversion API when adding conversion. Decide the source and meaning of your rate separately.

currency.js: precision and integer limits

The baseline here is currency.js 2.0.4. Its API uses integer-backed arithmetic and defaults to precision 2. Both precision and fromCents are configurable. Despite the option's name, fromCents respects the chosen precision instead of always dividing the input by 100. These behaviors are documented in the options and API reference and implemented in the release package.

import currency from 'currency.js';

currency(1999, { fromCents: true })
  .add(currency(160, { fromCents: true }))
  .intValue; // 2159

currency(100, { fromCents: true })
  .distribute(3)
  .map(value => value.intValue); // [34, 33, 33]

currency(1999, { fromCents: true, precision: 0 }).value; // 1999
currency(1234, { fromCents: true, precision: 3 }).value; // 1.234

The last two expressions are explicit zero- and three-decimal representations. Use the former to model the JPY example and the latter for the KWD example. The options do not attach a currency identity to the value. Your application must retain that identity and pass compatible options through its calculations. See currency.js internationalization and precision.

Integer-backed does not mean unlimited range. currency.js documents a safe-integer bound on its internal representation. Select precision together with the maximum expected amount, and check intermediate values as well as final totals. If the number representation is insufficient, evaluate Dinero's bigint interface or the separate arbitrary-precision decimal comparison.

Intl.NumberFormat: display without arithmetic

Intl.NumberFormat handles locale-sensitive presentation. It does not add currency-aware values or allocate a remainder. Supply the amount in the units your display contract expects, then choose the locale and currency. The ECMA-402 NumberFormat specification defines currency formatting and fraction-digit defaults.

const jpy = new Intl.NumberFormat('en-US', {
  style: 'currency',
  currency: 'JPY',
});
const kwd = new Intl.NumberFormat('en-US', {
  style: 'currency',
  currency: 'KWD',
});

jpy.resolvedOptions().maximumFractionDigits; // 0
kwd.resolvedOptions().maximumFractionDigits; // 3

These tested defaults show why one two-decimal display helper is insufficient. Inspect the currency-digit rules and set explicit options when your display requires something different. Do not infer an arithmetic policy from the formatter's output.

When connecting Dinero to a formatter, derive the decimal value from its representation. Its formatting guide explains the separation. Avoid a helper that receives only an unnamed integer and always applies /100; such a helper leaves both currency and scale implicit.

Storage and serialization without a universal cents rule

PostgreSQL documents NUMERIC and DECIMAL as equivalent exact numeric types. Decimal storage is therefore a valid option to evaluate, rather than something every application must reject. Select precision and scale deliberately: the numeric-type documentation also describes coercion to a declared scale.

For a Dinero value, the snapshot API exposes the full representation:

const snapshot = toSnapshot(total);
// {
//   amount: 2159,
//   currency: { code: 'USD', base: 10, exponent: 2 },
//   scale: 2
// }

const restored = dinero({
  amount: snapshot.amount,
  currency: snapshot.currency,
  scale: snapshot.scale,
});
toDecimal(restored); // '21.59'

Persist amount, currency and scale together, or document a schema that reconstructs all three without ambiguity. Preserve custom scales explicitly. A field named amount_cents plus a currency-code string is not the shape returned by toSnapshot. The scale documentation explains why currency exponent alone cannot recover every custom-scale value.

Treat serialization as a separate interface to verify. Write down the fields and numeric types your transport accepts, then test restoration against the original representation. For transport choices, see serialization and round-trip tradeoffs. The serializer choice should follow that contract.

Migration notes: replace obsolete API examples

Inventory existing imports and persisted values before changing calculations. For the pinned Dinero release, replace the obsolete currency-package import with dinero.js/currencies. Use add and toDecimal for the addition example, and allocate for the conserved split. Its published module exports those functions; it has no divide or singular toUnit export.

Do not migrate every stored amount by multiplying it by 100. First establish its current units, currency and scale. Record the target representation and compare restored values with the source before switching readers. Dinero's scale and snapshot contracts are the basis for this checklist, not a proposed database migration.

Keep the migration review concrete: which imports change, which fields become explicit, and which existing tests establish the intended value? Separate any conversion-rate work from representation changes. That makes mismatches easier to isolate before they affect stored data.

Tested examples and round-trip checklist

This original matrix records deterministic API checks against official pinned release tarballs on Node 26.13.0. It tests representation behavior, not speed, tax treatment or financial policy. The sources are the Dinero release, currency.js release and Intl specification.

CheckObserved resultWhat the check establishes
Dinero addition of the two USD amounts aboveDecimal string 21.59; snapshot amount 2159, scale 2Amount and scale remain explicit
Dinero allocation of 100 minor units[34, 33, 33]Shares conserve the total
currency.js addition with fromCentsInteger value 2159Minor-unit inputs produce the expected sum
currency.js distribution of 100 minor units[34, 33, 33]This split also conserves the total
currency.js precision 0 and 3 examplesValues 1999 and 1.234Input interpretation follows precision
Intl JPY and KWD default maximum fraction digits0 and 3Currency defaults differ

Use this persistence worksheet with your own fixtures:

  • Record the input amount, currency definition, scale and numeric type.
  • Calculate the expected snapshot before writing it.
  • Read the persisted data through the application's actual adapter.
  • Restore the value and compare every representation field.
  • Allocate a known total and confirm the shares sum to it.
  • Test zero, negative, maximum supported and custom-scale values separately.

For the bigint interface, add a transport check for that numeric type. Dinero's precision guide documents separate bigint imports and the absence of native JSON support. The number-based examples above do not establish a bigint serialization contract.

When to use which

Choose Dinero.js when explicit currency and scale should travel with each monetary value, and allocations belong in the same API. Trial the documented representation against your stored values before adopting it.

Choose currency.js when fixed precision and its chainable arithmetic fit your application, and you can own currency identity and input validation. Check its precision options and range limits against actual inputs.

Choose Intl.NumberFormat when the task is display. It can follow either arithmetic approach. Keep the formatter's locale options separate from calculation and persistence decisions.

Frequently Asked Questions

Can Intl.NumberFormat do money arithmetic?

It is the display formatter. Calculate or allocate with the selected representation first, then format the result using its currency and scale. See NumberFormat and Dinero scale.

Does every currency use cents?

No. Precision is part of the representation. The examples use precision 0 for JPY and 3 for KWD instead of assuming /100 for every currency. See currency.js options and the currency-formatting specification.

Does Dinero.js provide currency conversion?

Yes. convert accepts the target currency and rates you supply. It does not fetch live exchange rates.

What must I persist from a Dinero snapshot?

Preserve amount, currency and scale. The snapshot currency is an object with code, base and exponent, not just a currency-code string. See toSnapshot and custom scales.

Does integer-backed storage allow arbitrary-size amounts?

Not with an unrestricted JavaScript number. currency.js documents a safe-integer precision limit. Dinero has a documented bigint interface for larger representations.

Methodology

The package baselines are dinero.js 2.0.2 and currency.js 2.0.4. Their registry manifests declare MIT licenses and bundled types. Release history alone is not evidence of abandonment or security. The examples use pinned official release bodies; the matrix and migration worksheet are editorial synthesis. No comparative performance or compliance assessment was performed.

Source notes

Continue with arbitrary-precision decimal libraries for decimal arithmetic, or serialization choices for transport. Inspect currency.js package metadata and the currency.js and Dinero.js package comparison for package context.