From 14975381c2a6489513e68076e05193e786a40f16 Mon Sep 17 00:00:00 2001 From: rajanpanth Date: Sun, 23 Aug 2026 20:56:25 +0545 Subject: [PATCH] fix(drafting): keep the group separator off the minus sign draftIntegerFormat and draftDoubleFormat group the integer part by repeatedly taking its last three characters while more than three remain. The minus sign is part of that string, so it counted as a digit and a separator was inserted directly after it whenever the number of digits was a multiple of three: getDrafter('Integer')(-123, '0,0') // '-,123' getDrafter('Integer')(-123456, '0,0') // '-,123,456' getDrafter('Double')(-123456.78, '0,0.00')// '-,123,456.78' Numbers such as -1234 were unaffected, because the sign plus four digits left a two character remainder, which hid the bug for the lengths people usually try. The sign is now separated before grouping and prepended afterwards. Long shares Integer's implementation, so it is fixed too. Signed-off-by: rajanpanth --- src/drafting/Double/format.ts | 6 +++++- src/drafting/Integer/format.ts | 6 +++++- test/DraftFormat.test.ts | 21 +++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/drafting/Double/format.ts b/src/drafting/Double/format.ts index 73bab7f..fccd869 100644 --- a/src/drafting/Double/format.ts +++ b/src/drafting/Double/format.ts @@ -47,11 +47,15 @@ export function draftDoubleFormat(value:number,format:NumberDraftFormat) : strin res += sep2 + d; } let i = vs.substring(0,vs.length - (len === 0 ? 0 : len+1)); + // Group the digits only. Leaving the minus sign in place would let it + // be counted as a digit, so -123 would be grouped as -,123. + const sign = i.startsWith('-') ? '-' : ''; + i = i.substring(sign.length); while (i.length > 3) { res = sep1 + i.substring(i.length - 3) + res; i = i.substring(0, i.length - 3); } - return i + res; + return sign + i + res; }); } } \ No newline at end of file diff --git a/src/drafting/Integer/format.ts b/src/drafting/Integer/format.ts index 085359c..d98730b 100644 --- a/src/drafting/Integer/format.ts +++ b/src/drafting/Integer/format.ts @@ -40,11 +40,15 @@ export function draftIntegerFormat(value:number,format:NumberDraftFormat) : stri const vs = value.toFixed(0); let res = ''; let i = vs.substring(0,vs.length); + // Group the digits only. Leaving the minus sign in place would let it + // be counted as a digit, so -123 would be grouped as -,123. + const sign = i.startsWith('-') ? '-' : ''; + i = i.substring(sign.length); while (i.length > 3) { res = sep1 + i.substring(i.length - 3) + res; i = i.substring(0, i.length - 3); } - return i + res; + return sign + i + res; }); } } \ No newline at end of file diff --git a/test/DraftFormat.test.ts b/test/DraftFormat.test.ts index 717fe05..5166d2e 100644 --- a/test/DraftFormat.test.ts +++ b/test/DraftFormat.test.ts @@ -21,6 +21,27 @@ describe('number format', ()=>{ expect(drafter(123, TextNumberDraftFormat)).toBe('One Hundred Twenty Three'); expect(drafter(123456, TextNumberDraftFormat)).toBe('One Lakh Twenty Three Thousand Four Hundred Fifty Six'); }); + test('should group a negative integer without a separator after the sign', ()=>{ + const drafter:any=getDrafter('Integer'); + expect(drafter(-123, '0,0')).toBe('-123'); + expect(drafter(-999, '0,0')).toBe('-999'); + expect(drafter(-123456, '0,0')).toBe('-123,456'); + expect(drafter(-1234, '0,0')).toBe('-1,234'); + }); + test('should group a negative long without a separator after the sign', ()=>{ + const drafter:any=getDrafter('Long'); + expect(drafter(-123456, '0,0')).toBe('-123,456'); + }); + test('should group a negative double without a separator after the sign', ()=>{ + const drafter:any=getDrafter('Double'); + expect(drafter(-123, '0,0.00')).toBe('-123.00'); + expect(drafter(-123456.78, '0,0.00')).toBe('-123,456.78'); + expect(drafter(-1234.5, '0,0.00')).toBe('-1,234.50'); + }); + test('should still group positive numbers', ()=>{ + expect((getDrafter('Integer') as any)(123456, '0,0')).toBe('123,456'); + expect((getDrafter('Double') as any)(123456.78, '0,0.00')).toBe('123,456.78'); + }); test('should format double to words', ()=>{ const drafter:any=getDrafter('Double'); expect(drafter(123.045, TextNumberDraftFormat)).toBe('One Hundred Twenty Three Point Zero Four Five');