Issue #117: Done.

This commit is contained in:
chiayin
2023-09-19 17:53:24 +08:00
parent f73979d263
commit d1d0585269
2 changed files with 22 additions and 2 deletions

View File

@@ -0,0 +1,19 @@
/**
* 數量單位轉換,輸出 k m b t 數值。
* @param {number} number
* @returns {string}
*/
export default function shortScaleNumber(number) {
const abbreviations = ["", "k", "m", "b", "t"];
let index = 0;
let num = number;
// 確保在轉換數字時,不會超出索引範圍。如果 index 已經達到了最後一個單位縮寫t那麼我們就不再進行轉換以免超出數組的範圍。
while (num >= 1000 && index < abbreviations.length - 1) {
num /= 1000;
index++;
}
// 使用 Math.ceil 來無條件進位
num = Math.ceil(num * 10) / 10;
return num + abbreviations[index] + " " ;
}