diff options
Diffstat (limited to 'src/lib/utils.ts')
-rw-r--r-- | src/lib/utils.ts | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/lib/utils.ts b/src/lib/utils.ts index f93f68e..c2a955a 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -132,4 +132,19 @@ export function twemojiHtml(s: string) { return `<img src="/emoji/${[...match].map(p => p.codePointAt(0).toString(16)).join('-')}.svg" class="emoji">` }) return asTwemoji +} + +export function formatNumber(n: number, digits = 3) { + // from https://stackoverflow.com/a/9462382 with some modifications + const numberSymbolsLookup = [ + { value: 1, symbol: '' }, + { value: 1e3, symbol: 'k' }, + { value: 1e6, symbol: 'M' }, + { value: 1e9, symbol: 'G' }, + { value: 1e12, symbol: 'T' }, + { value: 1e15, symbol: 'P' }, + { value: 1e18, symbol: 'E' }, + ] + const item = numberSymbolsLookup.slice().reverse().find(item => n >= item.value) + return (n / item.value).toPrecision(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + item.symbol }
\ No newline at end of file |