更简单的计算数字乘数后缀的方法

时间:2015-10-22 15:16:14

标签: javascript jquery numbers exponent

function num(n) {
  if (n >= 1000 && n < 10000) return (n/1000).toFixed(3) + "K";
  if (n >= 10000 && n < 100000) return (n/1000).toFixed(1) + "K";
  if (n >= 100000 && n < 1000000) return (n/1000).toFixed(0) + "K";
  if (n >= 1000000 && n < 10000000) return (n/1000000).toFixed(3) + "M";
  if (n >= 10000000 && n < 100000000) return (n/1000000).toFixed(1) + "M";
  if (n >= 100000000 && n < 1000000000) return (n/1000000).toFixed(0) + "M";
  if (n >= 1000000000 && n < 10000000000) return (n/1000000000).toFixed(3) + "B";
  if (n >= 10000000000 && n < 100000000000) return (n/1000000000).toFixed(1) + "B";
  if (n >= 100000000000 && n < 1000000000000) return (n/1000000000).toFixed(0) + "B";
  if (n >= 1000000000000 && n < 10000000000000) return (n/1000000000000).toFixed(3) + "T";
  if (n >= 10000000000000 && n < 100000000000000) return (n/1000000000000).toFixed(1) + "T";
  if (n >= 100000000000000 && n < 1000000000000000) return (n/1000000000000).toFixed(0) + "T";
  return n;
}

因为在某些时候我可能会上升到数百人的力量,是否有更简单的方法来做到这一点?

4 个答案:

答案 0 :(得分:4)

function formatNumber(number) {
    var i = 0; units = [ "", "K", "M", "B", "T" ]; // etc
    while (number > 1000) {
        number /= 1000;
        i += 1;
    }
    return Math.floor(number * 1000) / 1000 + units[i];
}

formatNumber(1234567); // 1.234M
formatNumber(1230567); // 1.23M

对于非常大的数字,这可能会更快:

function formatNumber(number) {
    var i; units = [ "", "K", "M", "B", "T" ]; // etc
    i = Math.round(Math.log(number) / Math.log(10) / 3);
    number /= Math.pow(10, i * 3);
    return Math.floor(number * 1000) / 1000 + units[i];
}

formatNumber(1234567); // 1.234M
formatNumber(1230567); // 1.23M

答案 1 :(得分:0)

我的方法在这里:

function num(n) {
  if(n < 1000) {
    return n
  }

  var units = ['K', 'M', 'B', 'T']
  for(var i = -1; n >= 1000; i++) {
    n /= 1000
  }

  if(n >= 100) {
    return n.toFixed(0) + units[i]
  }
  if(n >= 10) {
    return n.toFixed(1) + units[i]
  }
  return n.toFixed(3) + units[i]
}

答案 2 :(得分:0)

这是在Halcyon的帮助下完成的功能。

function fnum(number) {
    var i, x, units = [ "", "K", "M", "B", "T", "Qa", "Qi", "Sx", "Sp", "Oc", "No", "Dc" ]; 
    i = Math.floor(Math.log(number) / Math.log(10) / 3);
    x = Math.floor(Math.log(number) / Math.log(10)) % 3;
    number /= Math.pow(10, i * 3);
    return ((number * 1000) / 1000).toFixed(77 % ((x * 2) + 3)) + units[i];
}

格式化为1s和10s时,将返回0到999到2位小数的数字,格式化为100s时返回0位小数,然后添加数字后缀。根据需要添加其他后缀。

fnum(22000); >> 22.00K fnum(153000000); >> 153M fnum(2,230,000,000,000); >> 2.23T

答案 3 :(得分:0)

function fnum(number) {
    var i, x, units = [ "", "K", "M", "B", "T", "Qa", "Qi", "Sx", "Sp", "Oc", "No", "Dc" ]; 
    i = Math.floor(Math.log(number) / Math.log(10) / 3);
    x = Math.floor(Math.log(number) / Math.log(10)) % 3;
    number /= Math.pow(10, i * 3);
    return ((number * 1000) / 1000).toFixed(77 % ((x * 2) + 3)) + units[i];
}
console.log(fnum(22000))
console.log(fnum(153000000))
console.log(fnum(2200000000000))