例如,我需要将6.688689舍入到6.7,但它总是显示7。
我的方法:
Math.round(6.688689);
//or
Math.round(6.688689, 1);
//or
Math.round(6.688689, 2);
但结果总是一样的7…我做错了什么?
例如,我需要将6.688689舍入到6.7,但它总是显示7。
我的方法:
Math.round(6.688689);
//or
Math.round(6.688689, 1);
//or
Math.round(6.688689, 2);
但结果总是一样的7…我做错了什么?
当前回答
Number((6.688689).toFixed(1)); // 6.7
其他回答
float(value,ndec);
function float(num,x){
this.num=num;
this.x=x;
var p=Math.pow(10,this.x);
return (Math.round((this.num).toFixed(this.x)*p))/p;
}
var number = 6.688689;
var roundedNumber = Math.round(number * 10) / 10;
Number((6.688689).toFixed(1)); // 6.7
我认为下面的功能可以帮助
function roundOff(value,round) {
return (parseInt(value * (10 ** (round + 1))) - parseInt(value * (10 ** round)) * 10) > 4 ? (((parseFloat(parseInt((value + parseFloat(1 / (10 ** round))) * (10 ** round))))) / (10 ** round)) : (parseFloat(parseInt(value * (10 ** round))) / ( 10 ** round));
}
用法:roundOff(600.23458,2);会返回600.23吗
+((6.688689 * (1 + Number.EPSILON)).toFixed(1)); // 6.7
+((456.1235 * (1 + Number.EPSILON)).toFixed(3)); // 456.124