Limit large numbers to 3 sig figs

This commit is contained in:
Austin Chen 2022-05-20 15:25:32 -04:00
parent 52ad4c42b0
commit 03a13248a4
2 changed files with 16 additions and 2 deletions

View File

@ -29,6 +29,20 @@ export function formatPercent(zeroToOne: number) {
return (zeroToOne * 100).toFixed(decimalPlaces) + '%' return (zeroToOne * 100).toFixed(decimalPlaces) + '%'
} }
// Eg 1234567.89 => 1.23M; 5678 => 5.68K
export function formatLargeNumber(num: number, sigfigs = 3): string {
const absNum = Math.abs(num)
if (absNum < 1000) {
return num.toPrecision(sigfigs)
}
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 `${numStr}${suffixStr}`
}
export function toCamelCase(words: string) { export function toCamelCase(words: string) {
const camelCase = words const camelCase = words
.split(' ') .split(' ')

View File

@ -1,7 +1,7 @@
import clsx from 'clsx' import clsx from 'clsx'
import Link from 'next/link' import Link from 'next/link'
import { Row } from '../layout/row' import { Row } from '../layout/row'
import { formatPercent } from 'common/util/format' import { formatLargeNumber, formatPercent } from 'common/util/format'
import { import {
Contract, Contract,
contractPath, contractPath,
@ -255,7 +255,7 @@ export function NumericResolutionOrExpectation(props: {
) : ( ) : (
<> <>
<div className="text-3xl text-blue-400"> <div className="text-3xl text-blue-400">
{getExpectedValue(contract)} {formatLargeNumber(getExpectedValue(contract))}
</div> </div>
<div className="text-base text-blue-400">expected</div> <div className="text-base text-blue-400">expected</div>
</> </>