adjust sig figs

This commit is contained in:
mantikoros 2022-07-16 14:50:07 -05:00
parent 7feacbd961
commit a3975080a1

View File

@ -36,15 +36,17 @@ export function formatPercent(zeroToOne: number) {
// Eg 1234567.89 => 1.23M; 5678 => 5.68K // Eg 1234567.89 => 1.23M; 5678 => 5.68K
export function formatLargeNumber(num: number, sigfigs = 2): string { export function formatLargeNumber(num: number, sigfigs = 2): string {
const absNum = Math.abs(num) const absNum = Math.abs(num)
if (absNum < 1000) { if (absNum < 1) return num.toPrecision(sigfigs)
return '' + Number(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 suffix = ['', 'K', 'M', 'B', 'T', 'Q']
const suffixIdx = Math.floor(Math.log10(absNum) / 3) const i = Math.floor(Math.log10(absNum) / 3)
const suffixStr = suffix[suffixIdx]
const numStr = (num / Math.pow(10, 3 * suffixIdx)).toPrecision(sigfigs) const numStr = (num / Math.pow(10, 3 * i)).toPrecision(sigfigs)
return `${Number(numStr)}${suffixStr}` return `${numStr}${suffix[i]}`
} }
export function toCamelCase(words: string) { export function toCamelCase(words: string) {