From 83dcbdb25f2e05eef110c1d981dfb5a15f5722bf Mon Sep 17 00:00:00 2001 From: Cyano Hao Date: Fri, 20 Nov 2020 16:21:16 +0800 Subject: [PATCH] Unify n-digit large number grouping method --- .../src/app/pipes/download-count.pipe.ts | 23 +++++++++---------- wowup-electron/src/assets/i18n/zh-TW.json | 20 ++++++++-------- wowup-electron/src/assets/i18n/zh.json | 20 ++++++++-------- 3 files changed, 31 insertions(+), 32 deletions(-) diff --git a/wowup-electron/src/app/pipes/download-count.pipe.ts b/wowup-electron/src/app/pipes/download-count.pipe.ts index e2f61643..03372cfe 100644 --- a/wowup-electron/src/app/pipes/download-count.pipe.ts +++ b/wowup-electron/src/app/pipes/download-count.pipe.ts @@ -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(); } } diff --git a/wowup-electron/src/assets/i18n/zh-TW.json b/wowup-electron/src/assets/i18n/zh-TW.json index 2ab07a41..8d89377c 100644 --- a/wowup-electron/src/assets/i18n/zh-TW.json +++ b/wowup-electron/src/assets/i18n/zh-TW.json @@ -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": { diff --git a/wowup-electron/src/assets/i18n/zh.json b/wowup-electron/src/assets/i18n/zh.json index 74fb5feb..434ddc0d 100644 --- a/wowup-electron/src/assets/i18n/zh.json +++ b/wowup-electron/src/assets/i18n/zh.json @@ -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": {