Skip to content
Merged
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
14 changes: 9 additions & 5 deletions lib/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,22 @@ export function general (ret: (string | number)[], value: number | string, l10n:
ret.push(fixLocale(o, l10n));
}
else if (exp === 10) {
const o = v.toFixed(10)
.slice(0, 12)
.replace(/\.$/, '');
ret.push(fixLocale(o, l10n));
// 11 integer digits leave no room for decimals: round to an integer
const r = Math.round(v);
if (r < 1e11) {
ret.push(r);
}
else {
ret.push(...getExp(1, 11, l10n));
}
}
else if (Math.abs(exp) <= 9) {
if (num_dig.total <= 11) {
const o = round(v, 9).toFixed(num_dig.frac);
ret.push(fixLocale(o, l10n));
}
else if (exp === 9) {
ret.push(Math.floor(v));
ret.push(Math.round(v));
}
else if (exp >= 0 && exp < 9) {
ret.push(fixLocale(String(round(v, 9 - exp)), l10n));
Expand Down
9 changes: 9 additions & 0 deletions test/numfmt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,12 @@ test('Correct output for single bracket patterns', () => {
expect(numfmt('[blue]', 123.456)).toBe('123.456');
expect(numfmt('[orange]', 123.456, { throws: false })).toBe('######');
});

test('General rounds 10 and 11 digit numbers with a fraction', () => {
expect(numfmt('General', 4403928373.5)).toBe('4403928374');
expect(numfmt('General', -4511987749.6)).toBe('-4511987750');
expect(numfmt('General', 12345678901.5)).toBe('12345678902');
expect(numfmt('General', 99999999999.6)).toBe('1E+11');
expect(numfmt('General', -4403928373.5)).toBe('-4403928374');
expect(numfmt('General', -99999999999.6)).toBe('-1E+11');
});