const transNumberToShort = (value, decimal = 2) => {
const BASE = 10000;
const SIZES = ["", "万", "亿", "万亿"];
let i = undefined;
let str = "";
if (isNaN(value)) {
throw new Error("The input parameter is not a number.");
}
if (typeof decimal !== "number" || decimal < 0) {
throw new Error("The 'decimal' parameter should be a non-negative number.");
}
if (value < BASE) {
str = value;
} else {
i = Math.floor(Math.log(value) / Math.log(BASE));
str = `${((value / Math.pow(BASE, i))).toFixed(decimal)}${SIZES[i]}`;
}
return str;
};
参考文档