Issues #83: done.

This commit is contained in:
chiayin
2023-09-12 11:48:32 +08:00
parent 35a494b99e
commit 11eb320c55
3 changed files with 29 additions and 13 deletions

View File

@@ -1,14 +1,30 @@
/**
* 將數字轉換成簡寫的形式1k、1m、1b等
* @param {number} number
* 將數字轉換成簡寫的形式,設定 dhms 的數值
* @param {number} totalSeconds
* @returns {string}
*/
export default function abbreviateNumber(number) {
const SI_SYMBOLS = ["", "k", "m", "b", "t"];
const tier = Math.log10(Math.abs(number)) / 3 | 0;
const suffix = SI_SYMBOLS[tier];
const scale = Math.pow(10, tier * 3);
const scaledNumber = number / scale;
export default function abbreviateNumber(totalSeconds) {
let seconds = 0;
let minutes = 0;
let hours = 0;
let days = 0;
let result = '';
let symbols = ['d', 'h', 'm', 's'];
return Math.round(scaledNumber) + suffix;
}
totalSeconds = parseInt(totalSeconds);
if(!isNaN(totalSeconds)) {
seconds = totalSeconds % 60;
minutes = (Math.floor(totalSeconds - seconds) / 60) % 60;
hours = (Math.floor(totalSeconds / 3600)) % 24;
days = Math.floor(totalSeconds / (3600 * 24));
};
const units = [days, hours, minutes, seconds];
for(let i = 0; i < units.length; i++) {
if(units[i] > 0) result += units[i] + symbols[i];
}
if(totalSeconds === 0) result = '0s';
return result;
};