JavaScriptFundamental (ES6), part 1
Format currency with Intl.NumberFormat in JavaScript
137 format naira with Intl.NumberFormat
fairBuild a naira formatter with an explicit locale, and inspect it rather than trusting the string: read resolvedOptions, take it apart with formatToParts, and check that the parts rejoin into the whole. Show that the locale, not the currency, decides whether you get the ₦ sign or the letters NGN, and that a formatted amount is no longer a number.
Stuck? Three hints, smallest first
- 1. Construct the formatter once and reuse it.
new Intl.NumberFormat(...)per row is the slow way to do this. - 2.
formatToPartsreturns{ type, value }objects —currency,integer,group,decimal,fraction— which is what to assert against when the exact string depends on the platform. - 3.
maximumFractionDigits: 0removes the kobo, and rounds rather than truncating.
const naira = new Intl.NumberFormat('en-NG', { style: 'currency', currency: 'NGN' });
const whole = new Intl.NumberFormat('en-NG', { style: 'currency', currency: 'NGN', maximumFractionDigits: 0 });
const american = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'NGN' });
const plain = new Intl.NumberFormat('en-US');
const amount = 1234567.5;
const formatted = naira.format(amount);
const parts = naira.formatToParts(amount);
const types = parts.map((p) => p.type);
const rejoined = parts.map((p) => p.value).join('');
const groups = formatted.split(',').length - 1;
const rounded = whole.format(amount);
const hasFraction = whole.formatToParts(amount).some((p) => p.type === 'fraction');
const symbols = [naira.formatToParts(1)[0].value, american.formatToParts(1)[0].value];
const settings = naira.resolvedOptions();
console.log('naira :', formatted, '| whole:', rounded);
console.log('parts :', types);
console.log('same currency, other locale:', american.format(amount));
Output
9 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 9 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('grouping in a plain formatter is the one string safe to pin down exactly', plain.format(1234567), '1,234,567');
test('the formatter reports back what it settled on', [settings.locale, settings.currency], ['en-NG', 'NGN']);
test('the parts describe the shape, whatever this platform prints', types, ['currency', 'integer', 'group', 'integer', 'group', 'integer', 'decimal', 'fraction']);
test('and they rejoin into exactly the formatted string', rejoined, formatted);
test('seven figures means two group separators', groups, 2);
test('dropping the fraction digits removes those parts and rounds up', [hasFraction, rounded.endsWith('568')], [false, true]);
test('the locale decides the symbol: en-NG gives the sign, en-US the code', symbols, ['₦', 'NGN']);
test('a formatted amount is text, not arithmetic', Number.isNaN(Number(formatted)), true);
test('so keep the money as whole kobo and format only at the edge', naira.format(150500 / 100).includes('1,505'), true);
Intl.NumberFormat knows the separators, the digit grouping and the symbol placement for a locale, which is a large amount of data you should not be hand-rolling. Build the formatter once and reuse it: construction is the expensive part, formatting is cheap.
The assertions here deliberately avoid pinning down the whole currency string. Output depends on the ICU data compiled into the platform, and it does change between versions — spacing around a symbol has moved more than once. So assert what you actually depend on: resolvedOptions for the settings, formatToParts for the shape, the group count for the separators. On this build the formatter prints ₦1,234,567.50, and it is fine to look at that in the log without freezing it into a test.
Two things catch people out. The locale, not the currency, chooses between ₦ and NGN — same currency: 'NGN', different output, because en-US has no local sign for it. And a formatted string is a string: it has commas and a symbol in it, so Number(...) gives NaN. Keep money as an integer count of kobo through every calculation and format once, at the very edge, where it is about to be read.