澄清一下,问题是如何围绕这样的数字。即1.512应该舍入到1.5并且2.123应该舍入到2并且2.323应该舍入到2.5(javascript)
答案 0 :(得分:6)
[1.512, 2.123, 2.323].forEach(function(currentNumber){
console.log(Math.round(currentNumber * 2) / 2);
});
<强>输出强>
1.5
2
2.5
答案 1 :(得分:0)
试试这个:
var num = 1.222;
num = num.toFixed(1);
答案 2 :(得分:-1)
使用Math.round(x)函数舍入到最接近的整数值