decimal.js
Use this skill for decimal.js v10 arbitrary-precision decimal arithmetic: construction, config, rounding, money-safe patterns, serialization, and advanced math.
Workflow
- Inspect the local surface before changing code:
- Package:
decimal.js (and siblings decimal.js-light, bignumber.js, big.js if present).
- Version: target v10 (current
10.6.0); bundled decimal.d.ts (BigInt in types since 10.6.0).
- Domain: money/currency, scientific, or general — this drives
clone config and whether significant-digit precision alone is enough.
- Imports: default or named
Decimal from "decimal.js".
- Refresh docs when versions are unclear or work touches rounding, money, trig, or library isolation. Start from source-map.md.
- Route deeper detail:
- Install, constructor, immutability, Value types: setup-core.md.
- Arithmetic, compare, format, static helpers: arithmetic-api.md.
set/config/clone, rounding modes, modulo: config-rounding.md.
- Money, JSON/SSR/store transport: money-serialization.md.
- Trig, random/crypto, light vs full, MikeMcl ecosystem: advanced-ecosystem.md.
- Anti-patterns and upgrades: pitfalls.md.
- Match the project's constructor style (
Decimal vs domain clone). Do not call global Decimal.set inside published libraries.
- Verify with typecheck plus tests for string construction, rounding mode,
toDP/toFixed, and serialization round-trips.
Library Decision Tree
Need trig / ln / exp / non-integer pow / wide exponents?
→ decimal.js (this skill)
Exact add/mul until division, finance-first, smaller API?
→ consider bignumber.js (author guidance) — or stay on decimal.js with Money clone + toDP
Tiny bundle, basic DP only?
→ big.js or decimal.js-light
Using decimal.js for money anyway?
→ Decimal.clone({ precision: 28, rounding: … }) + construct from strings + round once with toDP/toFixed
Core Judgment
- Construct from strings (or BigInt / Decimal) for money and multi-digit values. Never
new Decimal(0.1 + 0.2) or Number arithmetic first.
- Instances are immutable. Chain methods; do not expect in-place mutation.
precision = significant digits for (almost) all arithmetic results — not currency decimal places. Use toDecimalPlaces / toDP (or toFixed for strings) for fixed fractional places.
- Prefer
Decimal.clone({…}) for libraries and multi-domain apps. Use Decimal.set only for app-wide boot config.
- Value equality:
.eq / .cmp, never === / ==.
- Serialize as strings (
toJSON / toFixed / toString). Avoid toNumber() for money or persisted values.
- Default rounding is
ROUND_HALF_UP (4). Pick explicitly for finance (HALF_UP / HALF_EVEN / jurisdiction rules).
- Configure via
set/config/clone — never assign Decimal.precision = … directly (skips validation).
Verification
Prefer repository-owned commands. Cover the relevant subset:
bun pm ls decimal.js (and light / big / bignumber if migrating).
- Typecheck for
Decimal.Value, Decimal.Config, clone constructors.
- Tests: string vs number construction,
eq vs ===, toDP/toFixed rounding modes, penny-allocation / tax edges, JSON hydrate (new Decimal(dto.amount)), signed zero if relevant.
- Bundle/size check only when choosing light vs full.
Report which checks ran, which did not, and version/config assumptions that remain.
1---2name: decimal-js3description: Build, review, debug, migrate, or plan arbitrary-precision decimal math with decimal.js v10. Use for decimal.js, Decimal, Decimal.clone, Decimal.set, precision, rounding modes, ROUND_HALF_UP, ROUND_HALF_EVEN, toDecimalPlaces, toFixed, plus, minus, times, dividedBy, money/currency math, BigInt decimals, NaN/Infinity handling, trig/exp/ln, decimal.js-light, and comparisons with big.js or bignumber.js.4---56# decimal.js78Use this skill for **decimal.js v10** arbitrary-precision decimal arithmetic: construction, config, rounding, money-safe patterns, serialization, and advanced math.910## Workflow11121. Inspect the local surface before changing code:13 - Package: `decimal.js` (and siblings `decimal.js-light`, `bignumber.js`, `big.js` if present).14 - Version: target **v10** (current `10.6.0`); bundled `decimal.d.ts` (BigInt in types since 10.6.0).15 - Domain: money/currency, scientific, or general — this drives `clone` config and whether significant-digit `precision` alone is enough.16 - Imports: default or named `Decimal` from `"decimal.js"`.172. Refresh docs when versions are unclear or work touches rounding, money, trig, or library isolation. Start from [source-map.md](references/source-map.md).183. Route deeper detail:19 - Install, constructor, immutability, Value types: [setup-core.md](references/setup-core.md).20 - Arithmetic, compare, format, static helpers: [arithmetic-api.md](references/arithmetic-api.md).21 - `set`/`config`/`clone`, rounding modes, modulo: [config-rounding.md](references/config-rounding.md).22 - Money, JSON/SSR/store transport: [money-serialization.md](references/money-serialization.md).23 - Trig, random/crypto, light vs full, MikeMcl ecosystem: [advanced-ecosystem.md](references/advanced-ecosystem.md).24 - Anti-patterns and upgrades: [pitfalls.md](references/pitfalls.md).254. Match the project's constructor style (`Decimal` vs domain `clone`). Do not call global `Decimal.set` inside published libraries.265. Verify with typecheck plus tests for string construction, rounding mode, `toDP`/`toFixed`, and serialization round-trips.2728## Library Decision Tree2930```31Need trig / ln / exp / non-integer pow / wide exponents?32 → decimal.js (this skill)3334Exact add/mul until division, finance-first, smaller API?35 → consider bignumber.js (author guidance) — or stay on decimal.js with Money clone + toDP3637Tiny bundle, basic DP only?38 → big.js or decimal.js-light3940Using decimal.js for money anyway?41 → Decimal.clone({ precision: 28, rounding: … }) + construct from strings + round once with toDP/toFixed42```4344## Core Judgment4546- **Construct from strings (or BigInt / Decimal)** for money and multi-digit values. Never `new Decimal(0.1 + 0.2)` or Number arithmetic first.47- Instances are **immutable**. Chain methods; do not expect in-place mutation.48- **`precision` = significant digits** for (almost) all arithmetic results — not currency decimal places. Use **`toDecimalPlaces` / `toDP`** (or `toFixed` for strings) for fixed fractional places.49- Prefer **`Decimal.clone({…})`** for libraries and multi-domain apps. Use `Decimal.set` only for app-wide boot config.50- Value equality: **`.eq` / `.cmp`**, never `===` / `==`.51- Serialize as **strings** (`toJSON` / `toFixed` / `toString`). Avoid `toNumber()` for money or persisted values.52- Default rounding is **`ROUND_HALF_UP` (4)**. Pick explicitly for finance (`HALF_UP` / `HALF_EVEN` / jurisdiction rules).53- Configure via **`set`/`config`/`clone`** — never assign `Decimal.precision = …` directly (skips validation).5455## Verification5657Prefer repository-owned commands. Cover the relevant subset:5859- `bun pm ls decimal.js` (and light / big / bignumber if migrating).60- Typecheck for `Decimal.Value`, `Decimal.Config`, clone constructors.61- Tests: string vs number construction, `eq` vs `===`, `toDP`/`toFixed` rounding modes, penny-allocation / tax edges, JSON hydrate (`new Decimal(dto.amount)`), signed zero if relevant.62- Bundle/size check only when choosing light vs full.6364Report which checks ran, which did not, and version/config assumptions that remain.