我想缩短精确数量的追随者,并像社交平台一样以一种很好的方式展示它。问题是我的代码正在四舍五入最后一位数字。
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,我会阻止向上舍入,但会得到两位小数,这是不需要的。