js数字超过一万转换为万、亿

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;
};

参考文档

posted @ 2023-05-19 09:51  阿臻  阅读(405)  评论(0编辑  收藏  举报