[英]Javascript random infinite numbers
Right now I'm working on a ledger-type program in a web application. 现在,我正在研究Web应用程序中的分类帐程序。 I'm using angularJS to handle the events, but it's raw Javascript that handles the actual calculation
我正在使用angularJS来处理事件,但是它是处理实际计算的原始Javascript
var onEntriesSuccess = function(data) {
var sum = 0;
var isDebit = ($scope.account.active == "D" ? true : false);
angular.forEach(data, function(value, key){
sum += (isDebit ? value.debit - value.credit : value.credit - value.debit);
value.balance = sum;
});
$scope.entries = data;
}
Which seems to work fine; 似乎工作正常; however, while running it against my test case I find that it fails because when the sum = 10496.82 and attempts to subtract 6912.00 it comes out with
但是,在针对我的测试用例运行它时,我发现它失败了,因为当总和= 10496.82并尝试减去6912.00时,结果为
3584.8199999999997
I've never encountered an error like this before, at first I thought it was floating point and switched the value from 6912.02 to 6912.00. 我以前从未遇到过这样的错误,起初我以为是浮点,所以将值从6912.02切换为6912.00。 However, that doesn't seem to fix anything.
但是,这似乎无法解决任何问题。 Could anyone please explain this?
谁能解释一下吗? Thanks
谢谢
I updated Your fiddle 我更新了你的小提琴
sum=parseFloat(sum.toPrecision(16))
I take this solution from: 我从以下解决方案:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.