计算数字乘数后缀的更简单方法
Easier way to calculate numeric multiplier suffix
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;
}
因为在某些时候我可能会上升到数百的幂,有没有更简单的方法来做到这一点?
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
我这里的做法:
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]
}
这是在 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];
}
将return一个0到999之间的数字格式化为1s和10s时保留2位小数,格式化为100s时保留0位小数,然后加上数字后缀。根据需要添加其他后缀。
fnum(22000); >> 22.00K
fnum(153000000); >> 153M
fnum(2,230,000,000,000); >> 2.23T
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))
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;
}
因为在某些时候我可能会上升到数百的幂,有没有更简单的方法来做到这一点?
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
我这里的做法:
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]
}
这是在 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];
}
将return一个0到999之间的数字格式化为1s和10s时保留2位小数,格式化为100s时保留0位小数,然后加上数字后缀。根据需要添加其他后缀。
fnum(22000); >> 22.00K
fnum(153000000); >> 153M
fnum(2,230,000,000,000); >> 2.23T
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))