diff --git a/frontend/src/utils/__tests__/pricing.formatScaled.spec.ts b/frontend/src/utils/__tests__/pricing.formatScaled.spec.ts new file mode 100644 index 0000000000..6885061bee --- /dev/null +++ b/frontend/src/utils/__tests__/pricing.formatScaled.spec.ts @@ -0,0 +1,25 @@ +import { describe, expect, it } from 'vitest' +import { formatScaled } from '../pricing' + +describe('formatScaled', () => { + it.each([ + [1e-10, 1, '$1e-10'], + [1.25e-10, 1, '$1.25e-10'], + [1e-20, 1, '$1e-20'], + [1e-16, 1_000_000, '$1e-10'], + [1e10, 1, '$10000000000'], + ])('preserves the magnitude of %s scaled by %s', (value, scale, expected) => { + expect(formatScaled(value, scale)).toBe(expected) + }) + + it.each([ + [0.000003, 1_000_000, 0, '$3'], + [0.000003, 1_000_000, 2, '$3.00'], + [1.25e-8, 1_000_000, 2, '$0.0125'], + [5e-8, 1_000_000, 0, '$0.05'], + [0, 1, 2, '$0.00'], + [null, 1_000_000, 0, '-'], + ])('retains ordinary pricing and padding for %s', (value, scale, digits, expected) => { + expect(formatScaled(value, scale, digits)).toBe(expected) + }) +}) diff --git a/frontend/src/utils/pricing.ts b/frontend/src/utils/pricing.ts index b2fbd2e65d..feef800041 100644 --- a/frontend/src/utils/pricing.ts +++ b/frontend/src/utils/pricing.ts @@ -12,7 +12,7 @@ */ export function formatScaled(value: number | null, scale: number, minFractionDigits = 0): string { if (value == null) return '-' - let s = (value * scale).toPrecision(10).replace(/\.?0+$/, '') + let s = Number((value * scale).toPrecision(10)).toString() if (minFractionDigits > 0 && !s.includes('e')) { const dot = s.indexOf('.') const digits = dot === -1 ? 0 : s.length - dot - 1