no value should use decimals (#48155)

This commit is contained in:
tomxwang 2022-04-28 22:04:57 +08:00 committed by GitHub
parent fee291c3c8
commit af8fbc16d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 0 deletions

View File

@ -103,6 +103,9 @@ describe('valueFormats', () => {
expect(toFixed(100.4, 2)).toBe('100.40');
expect(toFixed(100.5, 2)).toBe('100.50');
expect(toFixed(0, 1)).toBe('0.0');
expect(toFixed(0, 2)).toBe('0.00');
});
});

View File

@ -56,6 +56,10 @@ export function toFixed(value: number, decimals?: DecimalCount): string {
decimals = getDecimalsForValue(value);
}
if (value === 0) {
return value.toFixed(decimals);
}
const factor = decimals ? Math.pow(10, Math.max(0, decimals)) : 1;
const formatted = String(Math.round(value * factor) / factor);