diff --git a/lib/general.ts b/lib/general.ts index dff4fea..a0c09f6 100644 --- a/lib/general.ts +++ b/lib/general.ts @@ -52,10 +52,14 @@ 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) { @@ -63,7 +67,7 @@ export function general (ret: (string | number)[], value: number | string, l10n: 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)); diff --git a/test/numfmt.spec.ts b/test/numfmt.spec.ts index 77a9b5c..022e9f9 100644 --- a/test/numfmt.spec.ts +++ b/test/numfmt.spec.ts @@ -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'); +});