mirror of
https://github.com/LemmyNet/joinlemmy-site.git
synced 2024-11-25 05:41:16 +00:00
Use a better SI shortener
This commit is contained in:
parent
f73a890c7c
commit
1c95e4327d
1 changed files with 8 additions and 21 deletions
|
@ -4,26 +4,13 @@ export function getDocsLanguage(lang: string): string {
|
||||||
return DOCS_LANGUAGES.includes(lang) ? lang : "en";
|
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) {
|
export function numToSI(value: any) {
|
||||||
var newValue = value;
|
return SHORTNUM_SI_FORMAT.format(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;
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue