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');