formatPercent: always show at least one sig fig
This commit is contained in:
parent
47f95678bd
commit
ae0cb4fc8c
|
@ -18,10 +18,20 @@ export function formatWithCommas(amount: number) {
|
||||||
return formatter.format(amount).replace('$', '')
|
return formatter.format(amount).replace('$', '')
|
||||||
}
|
}
|
||||||
|
|
||||||
export function formatPercent(zeroToOne: number) {
|
const decimalPlaces = (x: number) => Math.ceil(-Math.log10(x)) - 2
|
||||||
// Show 1 decimal place if <2% or >98%, giving more resolution on the tails
|
|
||||||
const decimalPlaces = zeroToOne < 0.02 || zeroToOne > 0.98 ? 1 : 0
|
export function formatPercent(decimalPercent: number) {
|
||||||
return (zeroToOne * 100).toFixed(decimalPlaces) + '%'
|
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) {
|
export function toCamelCase(words: string) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user