diff --git a/src/tests/number.test.ts b/src/tests/number.test.ts index 15c14456..7ff81fd3 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 b39912e5..13688d74 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(