Minor Unit Label
Turn amounts stored as whole numbers, such as cents or yen, into currency labels for reports and receipts. The last digit stays accurate even when the amount is too large for a JavaScript number.
What it does
Give the library an integer amount, its currency and the number of decimal places your data source uses. It returns a label using the requested locale. The label keeps exactly that many fraction digits.
Quick start
Run these commands from the directory containing this README. They build the local package and install it into a scratch project.
npm pack
archive="$PWD/minor-unit-label-0.1.0.tgz"
demo_dir="$(mktemp -d)"
cd "$demo_dir"
npm init -y
npm install --offline --no-audit "$archive"
cat > example.cjs <<'EXAMPLE'
const { formatMinorUnits } = require('minor-unit-label');
console.log(formatMinorUnits('9007199254740993', { currency: 'USD', scale: 2 }));
EXAMPLE
node example.cjs
Expected output
$90,071,992,547,409.93
Common examples
formatMinorUnits(1250, { currency: 'JPY', scale: 0 }); // '¥1,250'
formatMinorUnits(-5n, { currency: 'USD', scale: 2 }); // '-$0.05'
formatMinorUnits('2599', { currency: 'EUR', scale: 2, locale: 'de-DE' }); // '25,99\u00a0€'
Use the source's documented scale. For a feed that stores 1234 to mean 12.34, use scale 2 even if the currency normally displays zero decimal places.
API
formatMinorUnits(amount, { currency, scale, locale }) returns a string.
amount is a safe JavaScript integer, a bigint of at most 40 decimal digits, or a string containing up to 40 ASCII digits, with an optional leading minus and no leading zeros except for zero itself. Zero displays without a minus.
currency is a three-letter ASCII code, case-insensitive.
scale is a required integer from 0 to 9. It sets the positions after the decimal in both the source interpretation and the output.
locale defaults to en-US and must be an Intl-supported language tag of at most 100 characters. Intl can resolve a tag to a supported parent locale.
The function throws for invalid input, including unsafe numbers and unsupported locales.
Requirements
Node.js 22 or later with Intl locale data. This library has no runtime dependencies.
Security and limitations
The library formats locally. It does not parse formatted labels, convert currencies, compute totals, verify currency membership or enforce payment rules. Handle the result as display text. Symbols, spacing and digits depend on the runtime's locale data.
Troubleshooting
If a digit was already lost while reading a JSON number, send the amount as a JSON string from the data source. Converting the rounded number to a string afterward cannot recover it.
If the decimal point is in the wrong place, check your feed's scale. If your locale is rejected, check the tag and the locale data available in your Node build.
Installation and support
Install the included .tgz archive using npm install --offline --ignore-scripts ./minor-unit-label-0.1.0.tgz from a fresh local project (Node.js 22+). Use the included archive and npx --no-install to avoid downloading a different version.
node -e "console.log(require('minor-unit-label').formatMinorUnits('9007199254740993',{currency:'USD',scale:2}))"
Use synthetic or redacted inputs in bug reports. Optional support: https://buymeacoffee.com/eztoolgo (no entitlement or subscription).
Download and run
Use the versioned download ZIP attached to the corresponding release. Extract the complete archive and open index.html for the local interface and setup steps. No account or subscription is required.