[英]Rounding issue in Math.round() & .toFixed()
I used below two methods:我使用了以下两种方法:
Number.prototype.myRound = function (decimalPlaces) {
var multiplier = Math.pow(10, decimalPlaces);
return (Math.round(this * multiplier) / multiplier);
};
alert((239.525).myRound(2));
Mathematically alert should be 239.53
but its giving 239.52
as output. So i tried using .toFixed()
function & i got proper answer.数学上警报应该是
239.53
但它给出239.52
作为 output。所以我尝试使用.toFixed()
function & 我得到了正确的答案。
But when i try to get answer for 239.575
it gives again wrong output.但是当我尝试获得
239.575
的答案时,它再次给出了错误的 output。
alert((239.575).toFixed(2));
Here output should be 239.58
instead its giving 239.57
.这里 output 应该是
239.58
而不是它给出的239.57
。
This error creating a bit difference in final output. So can anyone help me to sort this out?这个错误在最终 output 中造成了一些差异。所以有人可以帮我解决这个问题吗?
This method will give very correct round result. 此方法将给出非常正确的舍入结果。
function RoundNum(num, length) {
var number = Math.round(num * Math.pow(10, length)) / Math.pow(10, length);
return number;
}
Just call this method. 只需调用此方法即可。
alert(RoundNum(192.168,2));
Internally, 239.575 cannot be represented exactly. 在内部,239.575无法准确表示。 In binary, 0.575 would be something like 1/2 + 1/16 + 1/128 + 1/256 + ....
在二进制,0.575将是像1/2 + 1/16 + 1/128 + 1/256 + ....
It just so happens that, represented in binary, the result is slightly less than 239.575. 事实恰恰如此,以二进制表示,结果略低于 239.575。 Therefore,
Math.round
rounds down. 因此,
Math.round
向下Math.round
。
To demonstrate, try this: 要演示,试试这个:
alert(239.575 - 239.5)
You would expect the result to be 0.075, but instead you get 0.07499999999998863. 你会期望结果是0.075,但你得到0.07499999999998863。
Just use Math.round
只需使用
Math.round
function round(figureToRound){
var roundOff = Math.round((figureToRound* 100 ).toFixed(2))/100;
return roundOff;
}
console.log(round(1.005));
This will help the rounding off issue completely. 这将有助于彻底解决问题。
round()
will do the trick.Try This: round()
会做的伎俩。试试这个:
var v= Math.round(239.575 * 100) / 100;
alert(v);
The problem is probably floating point inaccuracy, thus you might get different results in different cases (different gathering of a number, different browsers etc.). 问题可能是浮点不准确,因此在不同的情况下(不同的数字收集,不同的浏览器等)可能会得到不同的结果。
See also this: toFixed(2) rounds "x.525" inconsistently? 另见: toFixed(2)轮次“x.525”不一致?
function bestRound(val, decimals){
decimals = decimals || 2;
var multiplier = Math.pow(10, decimals)
return Math.round((val * multiplier ).toFixed(decimals)) / multiplier;
}
bestRound(239.575 - 239.5) 0.08
bestRound(239.575) 239.58
bestRound(239.525) 239.53
bestRound(1.005) 1.01
I got this to simply overwrite it ->我得到这个只是为了覆盖它->
Number.prototype.toFixed = function(fractionDigits, returnAsString = true) {
var digits = parseInt(fractionDigits) || 0;
var num = Number(this);
if( isNaN(num) ) {
return 'NaN';
}
var sign = num < 0 ? -1 : 1;
if (sign < 0) { num = -num; }
digits = Math.pow(10, digits);
num *= digits;
//num = Math.round(num.toFixed(12));
num = Math.round( Math.round(num * Math.pow(10,12)) / Math.pow(10,12) );
var ret = sign * num / digits;
return (returnAsString ? ret.toString() : ret ); // tofixed returns as string always
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.