|
| 1 | +import React from "react"; |
| 2 | +import numbro from "numbro"; |
| 3 | + |
| 4 | +import BigNumber from "bignumber.js"; |
| 5 | + |
| 6 | +export function formatExact(value: string | number) { |
| 7 | + return new BigNumber(value).toFormat(); |
| 8 | +} |
| 9 | + |
| 10 | +export function formatToken( |
| 11 | + value: string | number, |
| 12 | + digits = 4, |
| 13 | + exactTip?: boolean, |
| 14 | + noTrim?: boolean, |
| 15 | + // by default we truncate for tokens |
| 16 | + round?: boolean, |
| 17 | + exact?: boolean |
| 18 | +): React.ReactNode { |
| 19 | + const bn = new BigNumber(value); |
| 20 | + if (exact) { |
| 21 | + return formatExact(value); |
| 22 | + } |
| 23 | + if ( |
| 24 | + bn.isLessThan(1 / 10 ** digits) && |
| 25 | + !bn.isLessThanOrEqualTo(new BigNumber(0)) |
| 26 | + ) { |
| 27 | + return `< ${1 / 10 ** digits}`; |
| 28 | + } |
| 29 | + |
| 30 | + const usedValue = round ? value : bn.toFormat(digits, 1); |
| 31 | + |
| 32 | + const contents = numbro(usedValue).format({ |
| 33 | + thousandSeparated: true, |
| 34 | + trimMantissa: !noTrim, |
| 35 | + optionalMantissa: !noTrim, |
| 36 | + mantissa: digits, |
| 37 | + }); |
| 38 | + |
| 39 | + return contents; |
| 40 | +} |
| 41 | + |
| 42 | +export function formatRoundedToken( |
| 43 | + value: string | number, |
| 44 | + rounded?: boolean |
| 45 | +): string { |
| 46 | + const bn = new BigNumber(value); |
| 47 | + if (bn.isLessThan(0.0001) && !bn.isLessThanOrEqualTo(new BigNumber(0))) { |
| 48 | + return "< 0.0001"; |
| 49 | + } |
| 50 | + |
| 51 | + return bn.toFormat( |
| 52 | + !bn.isLessThan(1000) || rounded ? 0 : 2, |
| 53 | + 4 // ROUND_HALF_UP |
| 54 | + ); |
| 55 | +} |
| 56 | + |
| 57 | +export function formatUSD( |
| 58 | + value: string | number, |
| 59 | + omitPrefix?: boolean, |
| 60 | + rounded?: boolean, |
| 61 | + sigFigs?: number, |
| 62 | + noTrim?: boolean |
| 63 | +): string { |
| 64 | + const bn = new BigNumber(value); |
| 65 | + const neg = bn.isLessThan(0); |
| 66 | + const abs = bn.abs(); |
| 67 | + if (value === "0" && sigFigs) |
| 68 | + return `${neg ? "-" : ""}${omitPrefix ? "" : "$"}${Number(abs).toPrecision( |
| 69 | + sigFigs + 1 |
| 70 | + )}`; |
| 71 | + if (sigFigs && bn.lt(0.1)) { |
| 72 | + return `${neg ? "-" : ""}${omitPrefix ? "" : "$"}${Number(abs).toPrecision( |
| 73 | + sigFigs |
| 74 | + )}`; |
| 75 | + } |
| 76 | + if ( |
| 77 | + !noTrim && |
| 78 | + bn.isLessThan(0.01) && |
| 79 | + !bn.isLessThanOrEqualTo(new BigNumber(0)) |
| 80 | + ) { |
| 81 | + return `< ${omitPrefix ? "" : "$"}0.01`; |
| 82 | + } |
| 83 | + // When we have to do token price conversion into USD, we are often either too precise |
| 84 | + // or not precise enough to fully net a number back to 0. This accounts for that inaccuracy |
| 85 | + if (bn.abs().isLessThan(0.0001)) { |
| 86 | + return `${omitPrefix ? "" : "$"}0${rounded ? "" : ".00"}`; |
| 87 | + } |
| 88 | + |
| 89 | + return `${neg ? "-" : ""}${numbro(abs).format({ |
| 90 | + thousandSeparated: true, |
| 91 | + trimMantissa: false, |
| 92 | + mantissa: rounded ? 0 : 2, |
| 93 | + })}`; |
| 94 | +} |
| 95 | + |
| 96 | +// e.g. 2.3M or 4.3K |
| 97 | +// export function formatRoundedUSD(value: string | number): string { |
| 98 | +// if (new BigNumber(value).isLessThan(1000)) { |
| 99 | +// return formatUSD(value); |
| 100 | +// } |
| 101 | +// const mainLength = numeral(value).format('0a').length - 1; |
| 102 | +// return `$${numeral(value) |
| 103 | +// .format(`0.${Array(3 - mainLength + 1).join('0')}a`) |
| 104 | +// .toUpperCase()}`; |
| 105 | +// } |
| 106 | + |
| 107 | +export function formatPercent( |
| 108 | + value: string | number, |
| 109 | + noTrim?: boolean, |
| 110 | + limit?: number |
| 111 | +) { |
| 112 | + const bnPercent = new BigNumber(value).multipliedBy(100); |
| 113 | + if ( |
| 114 | + bnPercent.isLessThan(limit ?? 0.0001) && |
| 115 | + !bnPercent.isLessThanOrEqualTo(new BigNumber(0)) |
| 116 | + ) { |
| 117 | + return "< 0.01%"; |
| 118 | + } |
| 119 | + |
| 120 | + return numbro(value).format({ |
| 121 | + output: "percent", |
| 122 | + thousandSeparated: true, |
| 123 | + trimMantissa: !noTrim, |
| 124 | + optionalMantissa: !noTrim, |
| 125 | + mantissa: 2, |
| 126 | + }); |
| 127 | +} |
0 commit comments