Format number function
This commit is contained in:
parent
c939ab13c5
commit
ea55a8fdf4
|
|
@ -112,11 +112,17 @@ export const callUpdater = <T>(f: StateUpdater<T>, prev: T) =>
|
|||
typeof f === 'function' ? (f as Function)(prev) : f;
|
||||
|
||||
export const formatNumber = (n: number): string => {
|
||||
if (n === 0) return '0';
|
||||
if (n < 0) return `-${formatNumber(-n)}`;
|
||||
if (n >= 1_000_000_000_000) return `${(n / 1_000_000_000_000).toFixed(2)}T`;
|
||||
if (n >= 1_000_000_000) return `${(n / 1_000_000_000).toFixed(2)}B`;
|
||||
if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(2)}M`;
|
||||
if (n >= 1_000) return `${(n / 1_000).toFixed(2)}k`;
|
||||
return String(n);
|
||||
if (Math.trunc(n) === n) return n.toString();
|
||||
if (n >= 1) return n.toFixed(2);
|
||||
const result = n.toPrecision(2);
|
||||
if (parseFloat(result) >= 1) return parseFloat(result).toFixed(2);
|
||||
return result;
|
||||
};
|
||||
|
||||
export const formatTime = (seconds: number): string => {
|
||||
|
|
|
|||
|
|
@ -310,12 +310,27 @@ describe('utils', () => {
|
|||
});
|
||||
|
||||
describe('formatNumber', () => {
|
||||
it('should return small numbers as-is', () => {
|
||||
it('should return small integer as-is', () => {
|
||||
expect(formatNumber(0)).toBe('0');
|
||||
expect(formatNumber(42)).toBe('42');
|
||||
expect(formatNumber(999)).toBe('999');
|
||||
});
|
||||
|
||||
it('should return small decimals rounded to at most 2 significant digits', () => {
|
||||
expect(formatNumber(0.1)).toBe('0.10');
|
||||
expect(formatNumber(0.123)).toBe('0.12');
|
||||
expect(formatNumber(0.5)).toBe('0.50');
|
||||
expect(formatNumber(0.999)).toBe('1.00');
|
||||
expect(formatNumber(0.001)).toBe('0.0010');
|
||||
expect(formatNumber(0.0001234)).toBe('0.00012');
|
||||
});
|
||||
|
||||
it('should return decimals between 100 and 1000 with 2 decimal places', () => {
|
||||
expect(formatNumber(100.5)).toBe('100.50');
|
||||
expect(formatNumber(500.75)).toBe('500.75');
|
||||
expect(formatNumber(999.9)).toBe('999.90');
|
||||
});
|
||||
|
||||
it('should format thousands with k suffix', () => {
|
||||
expect(formatNumber(1_000)).toBe('1.00k');
|
||||
expect(formatNumber(5_500)).toBe('5.50k');
|
||||
|
|
@ -338,6 +353,16 @@ describe('utils', () => {
|
|||
expect(formatNumber(1_000_000_000_000)).toBe('1.00T');
|
||||
expect(formatNumber(5_250_000_000_000)).toBe('5.25T');
|
||||
});
|
||||
|
||||
it('should format negative numbers with a leading minus sign', () => {
|
||||
expect(formatNumber(-42)).toBe('-42');
|
||||
expect(formatNumber(-0.123)).toBe('-0.12');
|
||||
expect(formatNumber(-0.999)).toBe('-1.00');
|
||||
expect(formatNumber(-1_000)).toBe('-1.00k');
|
||||
expect(formatNumber(-1_000_000)).toBe('-1.00M');
|
||||
expect(formatNumber(-1_000_000_000)).toBe('-1.00B');
|
||||
expect(formatNumber(-1_000_000_000_000)).toBe('-1.00T');
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatTime', () => {
|
||||
|
|
|
|||
Loading…
Reference in New Issue