From 65a71b2d61e16acd04d6b0a719290f45e843c190 Mon Sep 17 00:00:00 2001 From: Jasper Behrensdorf Date: Fri, 20 Mar 2026 01:10:34 +0100 Subject: [PATCH] Correctly format single digit cent amounts Amounts of less than 10 cents where incorrectly formatted such that one cent would render as 0.1. The issue is fixed by padding the string with the appropriate number of zeros. I've also added a corresponding test. --- src/tests/number.test.ts | 5 +++++ src/utils/numbers.ts | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/tests/number.test.ts b/src/tests/number.test.ts index ab82a75a..3d191032 100644 --- a/src/tests/number.test.ts +++ b/src/tests/number.test.ts @@ -31,6 +31,11 @@ describe('getCurrencyHelpers', () => { expect(toUIString(value)).toBe('$1,234'); }); + it('Should correctly format single digit cent amounts', () => { + const value = 7n; + expect(toUIString(value)).toBe('$0.07'); + }); + it.each([ [12345n, '$123.45'], [-12345n, '-$123.45'], diff --git a/src/utils/numbers.ts b/src/utils/numbers.ts index 4d2f52dc..4fa31b50 100644 --- a/src/utils/numbers.ts +++ b/src/utils/numbers.ts @@ -147,7 +147,7 @@ export const getCurrencyHelpers = ({ if (typeof value === 'bigint') { const sign = value < 0n && signed ? '-' : ''; const integer = `${value / decimalMultiplierN}`; - const fraction = `${value}`.slice(-decimalDigits); + const fraction = `${value}`.slice(-decimalDigits).padStart(decimalDigits, '0'); return ( sign + normalizeToMaxLength(