Format kobo as naira currency in JavaScript
Intermediate · 3 min read
If your app stores money as whole numbers of kobo (100 kobo make one naira), sooner or later you have to show it to a person. 123,456,789 kobo should appear as ₦1,234,567.89: the naira sign, commas between thousands, a dot, and exactly two digits of kobo. Receipts, wallets, invoices and bank statements all need this.
In this tutorial you will write formatKobo(kobo) by hand, so you can see each part. You will handle amounts under one naira, refunds (negative amounts), and an input that should never have reached this function. You need to know what a function is, what % does (the remainder after dividing), and what Math.abs does (it removes a minus sign).
The parts of a naira amount
Take 123,456,789 kobo:
- Whole naira: divide by 100 and drop the decimals. That is 1,234,567.
- Kobo left over: the remainder after dividing by 100. That is 89.
- Grouping: put commas in the naira part:
1,234,567. - Sign: for a negative amount, a minus sign goes in front of everything:
-₦12,345.00.
Padding with padStart
padStart adds characters to the front of text until it reaches a length. It is how you turn 5 into 05:
console.log(String(5).padStart(2, '0')); // 05
console.log(String(89).padStart(2, '0')); // 89
Text that is already long enough is left alone.
Here is the exercise. Try it yourself first if you like, or read on and we will build it together.
74 Print kobo as naira
fairFormat an integer number of kobo as ₦1,234,567.89. Handle a negative amount, and an amount under one naira. Also exercise 48, which groups thousands; here the input is minor units and the sign has to be placed by hand.
Stuck? Three hints, smallest first
- 1. Split the kobo into whole naira and a remainder with a divmod, then pad the remainder to two digits.
- 2. Group the naira part with
toLocaleString, passing the locale explicitly. - 3. Take the absolute value first and put the minus sign back in front of the symbol, not in front of the digits.
Step 1: split naira and kobo
const kobo = 123456789;
const naira = Math.trunc(kobo / 100);
const rest = kobo % 100;
console.log(naira, rest); // 1234567 89
Math.trunc cuts off the decimal part of 1234567.89, leaving the whole naira. kobo % 100 is what is left over, the kobo.
Step 2: the padding bug
Join the two parts with a dot and try ₦10.05, which is 1005 kobo:
const kobo = 1005;
const naira = Math.trunc(kobo / 100);
const rest = kobo % 100;
console.log(naira + '.' + rest); // 10.5
10.5 reads as ten naira fifty, but the amount is ten naira five kobo. The remainder is the number 5, which has one digit. It needs to be padded to two:
const rest = 1005 % 100;
console.log('10.' + String(rest).padStart(2, '0')); // 10.05
This padding is not just for looks. Without it the displayed amount is wrong.
Step 3: group the thousands
toLocaleString formats a number the way a region writes it. Pass the locale 'en-NG' (English, Nigeria) so the result doesn't depend on the settings of whichever computer runs the code:
console.log((1234567).toLocaleString('en-NG')); // 1,234,567
console.log((0).toLocaleString('en-NG')); // 0
Step 4: put the sign in the right place
For a negative amount, use the size of the number for the math and add the minus sign back at the very front:
const SIGN = '\u20a6'; // the naira sign
function formatKobo(kobo) {
const negative = kobo < 0;
const size = Math.abs(kobo);
const naira = Math.trunc(size / 100);
const rest = size % 100;
const grouped = naira.toLocaleString('en-NG');
return (negative ? '-' : '') + SIGN + grouped + '.' + String(rest).padStart(2, '0');
}
console.log(formatKobo(123456789)); // ₦1,234,567.89
console.log(formatKobo(-1234500)); // -₦12,345.00
console.log(formatKobo(99)); // ₦0.99
console.log(formatKobo(1)); // ₦0.01
'\u20a6' is the code for the naira sign, ₦, which avoids typing it straight into the code. If you didn't take Math.abs first, the minus sign would come out between the naira sign and the digits, as ₦-12,345.00. Amounts under one naira work because Math.trunc(99 / 100) is 0.
Step 5: refuse a fraction of a kobo
A kobo amount should always be whole. If 4.35 arrives, some earlier code did math in naira when it should have used kobo. Rounding it here would hide that bug, so throw an error instead:
function checkKobo(kobo) {
if (!Number.isInteger(kobo)) throw new TypeError('kobo must be a whole number');
return kobo;
}
try {
checkKobo(4.35);
} catch (err) {
console.log(err.message); // kobo must be a whole number
}
In the full answer, this check is the first line of formatKobo.
In a real app you can also use Intl.NumberFormat with style: 'currency', which formats currency for you. Writing it by hand once shows what that built-in is doing.
The full solution
The answer panel has formatKobo with the whole-number check, formats five amounts including one kobo and a refund, and prints the refusal message. Its checks cover zero, padding of 1005 kobo, and grouping of millions. Press Run to see the output.
const SIGN = '\u20a6'; // the naira sign
function formatKobo(kobo) {
if (!Number.isInteger(kobo)) throw new TypeError('kobo must be a whole number');
const negative = kobo < 0;
const size = Math.abs(kobo);
const naira = Math.trunc(size / 100);
const rest = size % 100;
const grouped = naira.toLocaleString('en-NG');
return (negative ? '-' : '') + SIGN + grouped + '.' + String(rest).padStart(2, '0');
}
for (const k of [250000, 1, 99, 123456789, -1234500]) console.log(String(k).padStart(10), formatKobo(k));
let refused = '';
try { formatKobo(4.35); } catch (err) { refused = err.message; }
console.log('a fractional kobo:', refused);
Output
8 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 8 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('two and a half thousand naira', formatKobo(250000), SIGN + '2,500.00');
test('one kobo', formatKobo(1), SIGN + '0.01');
test('ninety-nine kobo', formatKobo(99), SIGN + '0.99');
test('millions are grouped', formatKobo(123456789), SIGN + '1,234,567.89');
test('a refund puts the sign outside the symbol', formatKobo(-1234500), '-' + SIGN + '12,345.00');
test('zero', formatKobo(0), SIGN + '0.00');
test('the kobo part is padded, not truncated', formatKobo(1005), SIGN + '10.05');
test('a fractional kobo is refused rather than rounded silently', refused.includes('whole number'), true);
String(rest).padStart(2, '0') is the whole reason this is written by hand: 5 kobo must print as .05, and the naive naira + '.' + rest gives 10.5, which is a hundred times the wrong amount. The padding is not cosmetic.
Taking Math.abs first and reattaching the sign keeps -₦12,345.00 rather than ₦-12,345.00. Refusing a fractional input is deliberate too: if a non-integer reaches here, some earlier code did naira arithmetic, and rounding it away at the display layer would hide that. Intl.NumberFormat with style: 'currency' does all of this for you and is the right choice in production — this version exists so the parts are visible.
Common mistakes
- Not padding the kobo. 1005 kobo prints as
10.5, which is ten naira fifty. - Leaving the minus on the digits.
₦-12,345.00looks wrong. Format the size, then put-in front of the sign. - Calling
toLocaleString()with no locale. The grouping then depends on the computer it runs on.
Try it yourself
- Format 50 kobo, 100 kobo and 100,000,000 kobo, and check each by hand.
- Add a second parameter so
formatKobo(250000, false)leaves out the naira sign. - Compare your output with
new Intl.NumberFormat('en-NG', { style: 'currency', currency: 'NGN' }).format(1234567.89).