From 1c95e4327d76e8d92bb7c73d35e4e317b0726e1f Mon Sep 17 00:00:00 2001 From: Dessalines Date: Wed, 8 Sep 2021 17:21:05 -0400 Subject: [PATCH] Use a better SI shortener --- src/shared/utils.ts | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/src/shared/utils.ts b/src/shared/utils.ts index 043a273..4a5f11b 100644 --- a/src/shared/utils.ts +++ b/src/shared/utils.ts @@ -4,26 +4,13 @@ export function getDocsLanguage(lang: string): string { return DOCS_LANGUAGES.includes(lang) ? lang : "en"; } +let SHORTNUM_SI_FORMAT = new Intl.NumberFormat("en-US", { + maximumFractionDigits: 1, + //@ts-ignore + notation: "compact", + compactDisplay: "short", +}); + export function numToSI(value: any) { - var newValue = value; - if (value >= 1000) { - var suffixes = ["", "k", "m", "b", "t"]; - var suffixNum = Math.floor(("" + value).length / 3); - var shortValue: any = ""; - for (var precision = 2; precision >= 1; precision--) { - shortValue = parseFloat( - (suffixNum != 0 - ? value / Math.pow(1000, suffixNum) - : value - ).toPrecision(precision) - ); - var dotLessShortValue = (shortValue + "").replace(/[^a-zA-Z 0-9]+/g, ""); - if (dotLessShortValue.length <= 2) { - break; - } - } - if (shortValue % 1 != 0) shortValue = shortValue.toFixed(1); - newValue = shortValue + suffixes[suffixNum]; - } - return newValue; + return SHORTNUM_SI_FORMAT.format(value); }