From 37bc5d5c07b119c4fa4c936e8a3b2b5f9588084b Mon Sep 17 00:00:00 2001 From: Bill Date: Tue, 22 Jun 2021 02:36:05 -0400 Subject: [PATCH] Add short number --- src/utils/LocalizeShortNumber.ts | 36 ++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/utils/LocalizeShortNumber.ts diff --git a/src/utils/LocalizeShortNumber.ts b/src/utils/LocalizeShortNumber.ts new file mode 100644 index 00000000..30975eca --- /dev/null +++ b/src/utils/LocalizeShortNumber.ts @@ -0,0 +1,36 @@ +export function LocalizeShortNumber(number: number): string +{ + if(!number || isNaN(number)) return '0'; + + let abs = Math.abs(number); + + const rounder = Math.pow(10, 1); + const isNegative = (number < 0); + + let key = ''; + + const powers = [ + { key: 'Q', value: Math.pow(10, 15) }, + { key: 'T', value: Math.pow(10, 12) }, + { key: 'B', value: Math.pow(10, 9) }, + { key: 'M', value: Math.pow(10, 6) }, + { key: 'K', value: 1000 } + ]; + + for(const power of powers) + { + let reduced = abs / power.value; + + reduced = Math.round(reduced * rounder) / rounder; + + if(reduced >= 1) + { + abs = reduced; + key = power.key; + + break; + } + } + + return ((isNegative ? '-' : '') + abs + key); +}