Files
lucia-frontend/src/module/abbreviateNumber.js

15 lines
427 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 將數字轉換成簡寫的形式1k、1m、1b等
* @param {number} number
* @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;
return Math.round(scaledNumber) + suffix;
}