Issue #117: Done.
This commit is contained in:
19
src/module/shortScaleNumber.js
Normal file
19
src/module/shortScaleNumber.js
Normal 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] + " " ;
|
||||
}
|
||||
Reference in New Issue
Block a user