javascript四舍五入保留两位小数不准确,有没有好的解决方案? 3.135.toFixed(2); 结果是3.13 我要的是3.14Math.round(2.135*100)/100; 结果是2.13 我要的结果是2.14 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 function round(num,point){ if(isNaN(num)){ return null; } point=Math.pow(10,point); num=num*point; if(num===+num){ return parseInt(num+0.5)/point; }else{ return +num; }}var a=2147483647.5657;console.log("result:"+round(a,2)); 修正:function round(num,point){ if(isNaN(num)){ return null; } point=Math.pow(10,point+1); num=num*point; if(num===+num){ return parseInt((num+5)/10)/point*10; }else{ return +num; }}var a=2.135;console.log("resu:"+round(a,2)); Math.ceil(3.135*100)/100 2.005变成2.0100000000000002 了把“ return parseInt((num+5)/10)/point*10; ”改成“ return parseInt((num+5)/10)/(point/10); ” Math.ceil(2.3401*100)/100结果为 2.35 但我要的是四舍五入为2.34 自己写一个js函数,将float类型数据装换成string的类型然后截取你省略的那位的后一位和5比较。将结果加上你使用toFixed的值就好了 四舍五入保留任意位的小数,如果有错请指正function roundFixed(num, fixed) { var pos = num.toString().indexOf('.'), decimal_places = num.toString().length - pos - 1, _int = num * Math.pow(10, decimal_places), divisor_1 = Math.pow(10, decimal_places - fixed), divisor_2 = Math.pow(10, fixed); return Math.round(_int / divisor_1) / divisor_2;}console.log(roundFixed(2147483647.5657,2));//-> 2147483647.57 var a = 3.135;alert((Math.round(a*100)/100).toFixed(2)) function formatFloat(src, pos){return Math.round(src*Math.pow(10, pos))/Math.pow(10, pos);}var num = 3.145;alert(formatFloat(num,2)) Math.ceil(2.3401*100)/100结果为 2.35 但我要的是四舍五入为2.34四舍五入用 Math.roundMath.floor() 向下取整Math.ceil() 向上取整(进1) formatFloat = num => isNaN(num) ? new Error('数字格式'):(Math.round(num * 100) / 100).toFixed(2)formatFloat (3.1901) //3.20formatFloat ('') //err JQUERY li问题 帮忙推荐基本javaScript的入门书 能否动态改变Script元素的代码? 如何实现从右往左滚动,起始位置不是从最右边开始? javascript数组属性length的使用方法? 如何改变鼠标样式 如何屏蔽关闭键(X),点X时不关闭 怎样检测cookie是否可写? JS,执行顺序问题。 jquery 翻页问题,请求高手解决! 前台怎么传一个对象数组到后台? 表单中input的value值想从JS 中获取 该怎么做呢? 查了很多资料还是没弄出来。
function round(num,point){
if(isNaN(num)){
return null;
}
point=Math.pow(10,point);
num=num*point;
if(num===+num){
return parseInt(num+0.5)/point;
}else{
return +num;
}
}
var a=2147483647.5657;
console.log("result:"+round(a,2));
if(isNaN(num)){
return null;
}
point=Math.pow(10,point+1);
num=num*point;
if(num===+num){
return parseInt((num+5)/10)/point*10;
}else{
return +num;
}
}
var a=2.135;
console.log("resu:"+round(a,2));
把“ return parseInt((num+5)/10)/point*10; ”
改成“ return parseInt((num+5)/10)/(point/10); ”
结果为 2.35 但我要的是四舍五入为2.34
将结果加上你使用toFixed的值就好了
var pos = num.toString().indexOf('.'),
decimal_places = num.toString().length - pos - 1,
_int = num * Math.pow(10, decimal_places),
divisor_1 = Math.pow(10, decimal_places - fixed),
divisor_2 = Math.pow(10, fixed);
return Math.round(_int / divisor_1) / divisor_2;
}console.log(roundFixed(2147483647.5657,2));//-> 2147483647.57
alert((Math.round(a*100)/100).toFixed(2))
function formatFloat(src, pos){
return Math.round(src*Math.pow(10, pos))/Math.pow(10, pos);
}
var num = 3.145;
alert(formatFloat(num,2))
结果为 2.35 但我要的是四舍五入为2.34四舍五入用 Math.round
Math.floor() 向下取整
Math.ceil() 向上取整(进1)