currency.js precise money math: working versions (tested 2026-10-16)
Build exact money helpers with currency.js: construct from string or number, add, subtract, multiply and distribute into whole cents, format with symbol and separator options, and prove there is no floating point drift offline.
passed6 passed · 0 failed2026-10-16 15:20 UTC- Status
- passed
- Last passed
- 2026-10-16 15:20 UTC
- Tests
- 6 / 6
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions3
currency.js@2.0.4@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false npm test
test environment
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38923127324
The code
Exactly the files that passed. Your agent can pull them with get_recipe("currency-js-money-math") (install), or see them on GitHub.
package.json
{
"name": "currency-js-money-math",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"currency.js": "2.0.4"
},
"devDependencies": {
"@types/node": "26.6.5",
"tsx": "4.23.15"
}
}
src/money.ts
import currency from "currency.js";
export type Money = ReturnType<typeof currency>;
export type MoneyOptions = NonNullable<Parameters<typeof currency>[1]>;
export function money(value: string | number, options?: MoneyOptions): Money {
return currency(value, options);
}
export function add(a: string | number, b: string | number): Money {
return currency(a).add(b);
}
export function subtract(a: string | number, b: string | number): Money {
return currency(a).subtract(b);
}
export function multiply(a: string | number, factor: number): Money {
return currency(a).multiply(factor);
}
export function distribute(total: string | number, parts: number): number[] {
return currency(total)
.distribute(parts)
.map((part) => part.value);
}
export function format(amount: string | number, options?: MoneyOptions): string {
return currency(amount, options).format();
}
test/money.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { money, add, subtract, multiply, distribute, format } from "../src/money.ts";
test("constructs from string and number", () => {
assert.equal(money("12.34").value, 12.34);
assert.equal(money(12.34).value, 12.34);
assert.equal(money("0.1").intValue, 10);
});
test("adds precise cents with no floating point drift", () => {
assert.notEqual(0.1 + 0.2, 0.3);
assert.equal(add("0.1", "0.2").value, 0.3);
assert.equal(add(0.1, 0.2).value, 0.3);
assert.equal(money(0.1).add(0.2).format(), "$0.30");
});
test("subtracts precise cents with no floating point drift", () => {
assert.notEqual(0.3 - 0.1, 0.2);
assert.equal(subtract("0.3", "0.1").value, 0.2);
assert.equal(subtract(0.3, 0.1).value, 0.2);
});
test("multiplies to whole cents", () => {
assert.notEqual(1.1 * 3, 3.3);
assert.equal(multiply("19.99", 3).value, 59.97);
assert.equal(multiply(1.1, 3).value, 3.3);
});
test("distributes a total into whole cents with no drift", () => {
const parts = distribute("10", 3);
assert.equal(parts.length, 3);
for (const part of parts) {
assert.equal(part, Number(part.toFixed(2)));
}
const total = Number(parts.reduce((sum, part) => sum + part, 0).toFixed(2));
assert.equal(total, 10);
assert.deepEqual(distribute(9, 3), [3, 3, 3]);
});
test("formats with symbol and separator options", () => {
assert.equal(format(1234.5, { symbol: "$", separator: "," }), "$1,234.50");
assert.equal(format(1234.5, { symbol: "USD ", separator: "," }), "USD 1,234.50");
assert.equal(format("0.1", { precision: 2 }), "$0.10");
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.