From 8ee4baf9ac55e2cc54c1699ecc51e7c83f6ac6b2 Mon Sep 17 00:00:00 2001 From: polskus <31725163+polskus@users.noreply.github.com> Date: Sun, 25 Oct 2026 00:45:45 +0200 Subject: [PATCH] Correctly format negative single digit cent amounts. Negative cent amounts from -1 to -9 were incorrectly formatted to be 0.1 and 0.9. The issue was the negative sign '-' which caused the padding to be off by one. A test including a negative number was also added. This completes the fix in #591. --- src/tests/number.test.ts | 8 +++++--- src/utils/numbers.ts | 7 +++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/tests/number.test.ts b/src/tests/number.test.ts index 15c14456a..7ff81fd3c 100644 --- a/src/tests/number.test.ts +++ b/src/tests/number.test.ts @@ -31,9 +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([ + [7n, '$0.07'], + [-7n, '$0.07'], + ])('Should correctly format single digit cent amounts', (value, expected) => { + expect(toUIString(value)).toBe(expected); }); it.each([ diff --git a/src/utils/numbers.ts b/src/utils/numbers.ts index b39912e58..13688d747 100644 --- a/src/utils/numbers.ts +++ b/src/utils/numbers.ts @@ -150,8 +150,11 @@ export const getCurrencyHelpers = ({ if (typeof value === 'bigint') { const sign = value < 0n && signed ? '-' : ''; - const integer = `${value / decimalMultiplierN}`; - const fraction = `${value}`.slice(-decimalDigits).padStart(decimalDigits, '0'); + const absValue = BigMath.abs(value); + const integer = `${absValue / decimalMultiplierN}`; + const fraction = `${absValue % decimalMultiplierN}` + .slice(-decimalDigits) + .padStart(decimalDigits, '0'); return ( sign + normalizeToMaxLength(