如何防止双精度错误地减去小数 [英] How do I prevent a double from doing incorrect subtraction of a decimal

查看:94
本文介绍了如何防止双精度错误地减去小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是在循环的第3个条目中得到错误的小数i。如果我使用计算器我得到了正确的结果,但如果我把它放在excel中我得到错误的十进制结果。我想要的是在excel和我的系统中获得相同的结果



例如。



my校长的优秀成绩是765,517.81,但是对于第3次入场我得到769,345.39





以下是我的代码。





Dim balance As Double = 25600000

Dim pmtmonthlyamortization As Double = 893517.81

Dim interestrate As Double = 6.0 / 100



Dim TotPmts As Double

Dim mprincipal As Double

Dim minterest As Double



TotPmts = CDbl(Val(5))



i = 0 To TotPmts

minterest =(余额*(利息/ 12))

mprincipal =(pmtmonthlyamortization - minterest)

balance =(balance - mprincipal)



MsgBox(minterest& vbNewLine& mprincipal& vbNewLine& vbNewLine& balance)

下一页



我尝试过的事情:



i已尝试使用DOUBLE,DECIMAL,LONG

i always get the wrong decimal in the 3rd entry of the loop i. If i use the calculator i get the right result but if i put it in excel i got the wrong result in decimal. what i want is to have the same result in excel and my system

for example.

my result in excel for the principal is 765,517.81 but for the 3rd entry i get 769,345.39


below is my code.


Dim balance As Double = 25600000
Dim pmtmonthlyamortization As Double = 893517.81
Dim interestrate As Double = 6.0 / 100

Dim TotPmts As Double
Dim mprincipal As Double
Dim minterest As Double

TotPmts = CDbl(Val(5))

For i = 0 To TotPmts
minterest = (balance * (interestrate / 12))
mprincipal = (pmtmonthlyamortization - minterest)
balance = (balance - mprincipal)

MsgBox(minterest & vbNewLine & mprincipal & vbNewLine & vbNewLine & balance)
Next

What I have tried:

i already tried using DOUBLE,DECIMAL,LONG

推荐答案

首先准确比较Excel对您的代码所做的事情:我们可以;看到你的表,所以我们不知道!



这个价值差异远远超过某种精确度问题,所以最多可能的解释很简单:这两个代码正在做不同的事情

如果你能立即看到它,那就开始仔细检查:



复制Excel单元格计算并将其拆分,使其位于多个单元格中,以准确模拟VB代码中的内容。结果是否与您的代码或Excel匹配?

如果它与您的代码匹配,那么单个单元格中的Excel计算不会达到您的预期效果!

如果它匹配工作表,然后查看压缩和扩展版本的差异。
Start by comparing exactly what Excel is doing with your code: we can;t see your sheet, so we have no idea!

And that difference in value is far, far too big to be some sort of precision problem, so the most likely explanation is simple: "the two codes are doing different things"
If you can see it immediately, then start examining closely:

Copy your Excel cell calculation and split it so it is in several cells, to mimic exactly what you have in your VB code. Does the result match your code, or Excel?
If it matches your code, then the excel calculation in a single cell does not do what you think it does!
If it matches the sheet, then look at the difference in the "compressed" and "expanded" versions.


这篇关于如何防止双精度错误地减去小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆