ittiam
2/25/2016 - 3:25 AM

数字单位化

数字单位化

function numericFormatter(num, digits) {
  var si = [
    { value: 1E18, symbol: "E" },
    { value: 1E15, symbol: "P" },
    { value: 1E12, symbol: "T" },
    { value: 1E8,  symbol: "亿" },
    { value: 1E7,  symbol: "千万" },
    { value: 1E6,  symbol: "百万" },
    //{ value: 1E4,  symbol: "万" },
    { value: 1E3,  symbol: "千" }
  ], i;
  digits = digits || 2;
  for (i = 0; i < si.length; i++) {
    if (num >= si[i].value) {
      return (num / si[i].value).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, "$1") + si[i].symbol;
    }
  }
  return num.toString();
}