Unify n-digit large number grouping method

This commit is contained in:
Cyano Hao
2020-11-20 16:21:16 +08:00
parent f4dc18b25d
commit 83dcbdb25f
3 changed files with 31 additions and 32 deletions

View File

@@ -8,24 +8,23 @@ import { TranslateService } from "@ngx-translate/core";
export class DownloadCountPipe implements PipeTransform {
constructor(private translateService: TranslateService) {}
shortenDownloadCount(value: number, nDigit: number): string {
const exponent = Math.log10(value);
const nGroups = Math.floor(exponent / nDigit);
const shortValue = value / Math.pow(10, nGroups * nDigit);
return shortValue.toFixed(0);
}
transform(value: number, ...args: unknown[]): string {
const numMatches = /(e\+\d+)/.exec(value.toExponential());
const suffix = numMatches[1];
let downloadCount = value;
if (downloadCount >= 1000000000) {
downloadCount /= 1000000000.0;
} else if (downloadCount >= 1000000) {
downloadCount /= 1000000.0;
} else if (downloadCount >= 1000) {
downloadCount /= 1000.0;
}
return suffix
? this.translateService.instant("COMMON.DOWNLOAD_COUNT." + suffix, {
count: downloadCount.toFixed(0),
simpleCount: value.toString()[0]
count: this.shortenDownloadCount(value, 3),
simpleCount: this.shortenDownloadCount(value, 1),
myriadCount: this.shortenDownloadCount(value, 4),
})
: downloadCount.toString();
: value.toString();
}
}

View File

@@ -65,16 +65,16 @@
"TOOLTIP": "{dependencyCount} 個依賴項"
},
"DOWNLOAD_COUNT": {
"e+0": "{count}",
"e+1": "{count}",
"e+2": "{count}",
"e+3": "{count} thousand",
"e+4": "{count} thousand",
"e+5": "{count} thousand",
"e+6": "{count} million",
"e+7": "{count} million",
"e+8": "{count} million",
"e+9": "{count} billion"
"e+0": "{myriadCount}",
"e+1": "{myriadCount}",
"e+2": "{myriadCount}",
"e+3": "{myriadCount}",
"e+4": "{myriadCount} 萬",
"e+5": "{myriadCount} 萬",
"e+6": "{myriadCount} 萬",
"e+7": "{myriadCount} 萬",
"e+8": "{myriadCount} 億",
"e+9": "{myriadCount} 億"
},
"ENUM": {
"ADDON_CHANNEL_TYPE": {

View File

@@ -65,16 +65,16 @@
"TOOLTIP": "{dependencyCount} 个依赖项"
},
"DOWNLOAD_COUNT": {
"e+0": "{count}",
"e+1": "{count}",
"e+2": "{count}",
"e+3": "{count} thousand",
"e+4": "{count} thousand",
"e+5": "{count} thousand",
"e+6": "{count} million",
"e+7": "{count} million",
"e+8": "{count} million",
"e+9": "{count} billion"
"e+0": "{myriadCount}",
"e+1": "{myriadCount}",
"e+2": "{myriadCount}",
"e+3": "{myriadCount}",
"e+4": "{myriadCount} 万",
"e+5": "{myriadCount} 万",
"e+6": "{myriadCount} 万",
"e+7": "{myriadCount} 万",
"e+8": "{myriadCount} 亿",
"e+9": "{myriadCount} 亿"
},
"ENUM": {
"ADDON_CHANNEL_TYPE": {