From 087e5f89fdb20aae9a1db9b00c118d85221ccb04 Mon Sep 17 00:00:00 2001 From: Austin Chen Date: Tue, 22 Mar 2022 16:53:23 -0700 Subject: [PATCH] Revert "formatPercent: always show at least one sig fig" This reverts commit ae0cb4fc8c7e9641216775adbaa9132c1c368580. --- common/util/format.ts | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/common/util/format.ts b/common/util/format.ts index 7f352cf2..05a8f702 100644 --- a/common/util/format.ts +++ b/common/util/format.ts @@ -18,20 +18,10 @@ export function formatWithCommas(amount: number) { return formatter.format(amount).replace('$', '') } -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 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) + '%' } export function toCamelCase(words: string) {