Commit 32b73bb4 by Ryan McKinley Committed by Torkel Ödegaard

ValueFormats: check for inf (#19376)

parent 94d7af88
import { toFixed, getValueFormat } from './valueFormats'; import { toFixed, getValueFormat } from './valueFormats';
describe('valueFormats', () => { describe('valueFormats', () => {
describe('toFixed with edge cases', () => {
it('should handle non number input gracefully', () => {
expect(toFixed(NaN)).toBe('NaN');
expect(toFixed(Number.NEGATIVE_INFINITY)).toBe('-Inf');
expect(toFixed(Number.POSITIVE_INFINITY)).toBe('Inf');
});
});
describe('toFixed and negative decimals', () => { describe('toFixed and negative decimals', () => {
it('should treat as zero decimals', () => { it('should treat as zero decimals', () => {
const str = toFixed(186.123, -2); const str = toFixed(186.123, -2);
......
...@@ -33,6 +33,12 @@ export function toFixed(value: number, decimals?: DecimalCount): string { ...@@ -33,6 +33,12 @@ export function toFixed(value: number, decimals?: DecimalCount): string {
if (value === null) { if (value === null) {
return ''; return '';
} }
if (value === Number.NEGATIVE_INFINITY) {
return '-Inf';
}
if (value === Number.POSITIVE_INFINITY) {
return 'Inf';
}
const factor = decimals ? Math.pow(10, Math.max(0, decimals)) : 1; const factor = decimals ? Math.pow(10, Math.max(0, decimals)) : 1;
const formatted = String(Math.round(value * factor) / factor); const formatted = String(Math.round(value * factor) / factor);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment