From 069b1287524eb59a519967b405c546ef16ad9b58 Mon Sep 17 00:00:00 2001 From: Ketan Reddy Date: Fri, 8 Mar 2024 12:09:21 -0800 Subject: [PATCH] fix 0 precision commaNumber formatting --- .../common-types/core/src/formats/__tests__/formats.test.ts | 3 +++ plugins/common-types/core/src/formats/index.ts | 6 ++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/plugins/common-types/core/src/formats/__tests__/formats.test.ts b/plugins/common-types/core/src/formats/__tests__/formats.test.ts index 66d8213d4..5e1a095c2 100644 --- a/plugins/common-types/core/src/formats/__tests__/formats.test.ts +++ b/plugins/common-types/core/src/formats/__tests__/formats.test.ts @@ -68,6 +68,9 @@ describe('commaNumber', () => { expect(commaNumber.format?.(1000.999, { precision: 2 })).toBe('1,000.99'); expect(commaNumber.format?.(1000, { precision: 2 })).toBe('1,000.00'); expect(commaNumber.format?.(1000.1, { precision: 2 })).toBe('1,000.10'); + expect(commaNumber.format?.(123456789, { precision: 0 })).toBe( + '123,456,789' + ); }); it('handles out of bounds', () => { diff --git a/plugins/common-types/core/src/formats/index.ts b/plugins/common-types/core/src/formats/index.ts index 5df3377de..359a0506c 100644 --- a/plugins/common-types/core/src/formats/index.ts +++ b/plugins/common-types/core/src/formats/index.ts @@ -115,7 +115,6 @@ export const commaNumber: FormatType< // Beautify preDecDigits = preDecDigits.replace(/\B(?=(\d{3})+(?!\d))/g, ','); - if (preDecDigits === '' && firstDecimal === 0) { preDecDigits = '0'; } @@ -127,7 +126,10 @@ export const commaNumber: FormatType< retVal = `-${retVal}`; } - if (firstDecimal >= 0 || options?.precision !== undefined) { + if ( + (firstDecimal >= 0 || options?.precision !== undefined) && + postDecDigits !== '' + ) { retVal += `.${postDecDigits}`; }