diff --git a/common/util/format.ts b/common/util/format.ts index 05a8f702..7f352cf2 100644 --- a/common/util/format.ts +++ b/common/util/format.ts @@ -18,10 +18,20 @@ export function formatWithCommas(amount: number) { return formatter.format(amount).replace('$', '') } -export function formatPercent(zeroToOne: number) { - // Show 1 decimal place if <2% or >98%, giving more resolution on the tails - const decimalPlaces = zeroToOne < 0.02 || zeroToOne > 0.98 ? 1 : 0 - return (zeroToOne * 100).toFixed(decimalPlaces) + '%' +const decimalPlaces = (x: number) => Math.ceil(-Math.log10(x)) - 2 + +export function formatPercent(decimalPercent: number) { + const displayedFigs = + (decimalPercent >= 0.02 && decimalPercent <= 0.98) || + decimalPercent <= 0 || + decimalPercent >= 1 + ? 0 + : Math.max( + decimalPlaces(decimalPercent), + decimalPlaces(1 - decimalPercent) + ) + + return (decimalPercent * 100).toFixed(displayedFigs) + '%' } export function toCamelCase(words: string) {