From a3975080a1421654c5514626ae5b9f57c41122fd Mon Sep 17 00:00:00 2001 From: mantikoros Date: Sat, 16 Jul 2022 14:50:07 -0500 Subject: [PATCH] adjust sig figs --- common/util/format.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/common/util/format.ts b/common/util/format.ts index decdd55d..7dc1a341 100644 --- a/common/util/format.ts +++ b/common/util/format.ts @@ -36,15 +36,17 @@ export function formatPercent(zeroToOne: number) { // Eg 1234567.89 => 1.23M; 5678 => 5.68K export function formatLargeNumber(num: number, sigfigs = 2): string { const absNum = Math.abs(num) - if (absNum < 1000) { - return '' + Number(num.toPrecision(sigfigs)) - } + if (absNum < 1) return num.toPrecision(sigfigs) + + if (absNum < 100) return num.toPrecision(2) + if (absNum < 1000) return num.toPrecision(3) + if (absNum < 10000) return num.toPrecision(4) const suffix = ['', 'K', 'M', 'B', 'T', 'Q'] - const suffixIdx = Math.floor(Math.log10(absNum) / 3) - const suffixStr = suffix[suffixIdx] - const numStr = (num / Math.pow(10, 3 * suffixIdx)).toPrecision(sigfigs) - return `${Number(numStr)}${suffixStr}` + const i = Math.floor(Math.log10(absNum) / 3) + + const numStr = (num / Math.pow(10, 3 * i)).toPrecision(sigfigs) + return `${numStr}${suffix[i]}` } export function toCamelCase(words: string) {