问题描述
我想简短地显示一定数量的关注者,并以一种很好的方式(如社交平台)来显示它。问题是我的代码将最后一位四舍五入。
function getShortFollowers(num){
function intlFormat(num){
return new Intl.NumberFormat().format(Math.round(num*10)/10);
}
if(num >= 1000000)
return intlFormat(num/1000000)+'M';
if(num >= 1000)
return intlFormat(num/1000)+'k';
return intlFormat(num);
}
// Result
console.log(getShortFollowers(28551) // output: 28.6
// Wanted result
console.log(getShortFollowers(28551) // output: 28.5
如果我将Math.round除以100而不是10,我可以防止四舍五入,但是会得到两位十进制数,这是不必要的。
解决方法
尝试这样。
kube-minion