Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/drafting/Double/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
}
6 changes: 5 additions & 1 deletion src/drafting/Integer/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
}
21 changes: 21 additions & 0 deletions test/DraftFormat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Loading