文本框数据-秘密编号并计算不工作,请帮助 [英] text box data - covert to number and calculate NOT WORKING please help
问题描述
我正在尝试允许我的应用程序的用户在文本框中输入数字数据,然后将1的数字加起来最多5,以验证它们是否相同. 所以我需要添加转换文本为数字,然后减去.该代码已经工作了几个月,直到有人尝试输入该代码才有问题 中的 89.63 tbxTicketAmount , 32.00 在 tbxAmount1 和 57.63 在 tbxAmount2 .这些金额应等于0,但不起作用.其他小数和/或整数可以正常工作. >
昏暗 x1 As Double
Dim 如 Double
Dim 如 Double
Dim 如 Double
Dim 如 Double
Dim 如 Double
Dim 如 Double
'为每个结果设置值
x1 = Val(tbxTicketAmount.Text)
amt1 = Val(tbxAmount1.Text)
amt2 = Val(tbxAmount2.Text)
amt3 = Val(tbxAmount3.Text)
amt4 = Val(tbxAmount4.Text)
amt5 = Val(tbxAmount5.Text)
'减去值,必须回答0
bal = x1-amt1-amt2-amt3-amt4-amt5
如果 然后
lblBalance.Text = "[!]交易不平衡"
lblSUBMIT.Text = "[!]交易不平衡"
其他
在计算机中,单精度或双精度数字会发生(或不发生)一定的舍入.因此,如果您看一下您认为是整数的值,则可以是3.999999999(..)而不是4.我怀疑您没有得到完全为零的值,而是得到0.000000000000012. 或者其他的东西.如果您查看bal的值,则该值不为零.
一种解决方法是像这样使用零值范围:
昏暗公差为Double = 0.000001 如果bal>容忍度,并且bal<然后容忍 如果结束
PS如果您只有百分位数,则使用十进制类型可能会有所帮助.
I am trying to allow users of my application to enter numeric data in a textbox then add up to 5 amounts against 1 to verify they are the same. so I need to add convert text to numbers then subtract. the code has been working for months, no issue until someone tried entering 89.63 in tbxTicketAmount, 32.00 in tbxAmount1 and 57.63 in tbxAmount2. these amounts should equal 0 but it does not work. other decimals and/or whole numbers work fine.
Dim x1 As Double
Dim amt1 As Double
Dim amt2 As Double
Dim amt3 As Double
Dim amt4 As Double
Dim amt5 As Double
Dim bal As Double
'set values for each result
x1 = Val(tbxTicketAmount.Text)
amt1 = Val(tbxAmount1.Text)
amt2 = Val(tbxAmount2.Text)
amt3 = Val(tbxAmount3.Text)
amt4 = Val(tbxAmount4.Text)
amt5 = Val(tbxAmount5.Text)
'subtract values, must answer 0
bal = x1 - amt1 - amt2 - amt3 - amt4 - amt5
If bal <> 0 Then
lblBalance.Text = "[!] Transaction does not balance"
lblSUBMIT.Text = "[!] Transaction does not balance"
Else
In a computer a single or double precision number has a certain rounding that occurs (or does not). So if you look a the value of what you think is an integer it can be 3.999999999(..) instead of 4. I suspect you dont get exactly zero you get 0.000000000000012 or something. If you view the value of bal it is not zero.
One work around is just use a range of values for zero like this:
Dim tolerance As Double = 0.000001 If bal > -tolerance AndAlso bal < tolerance Then End If
PS Using a decimal type may help if you just have hundreths.
这篇关于文本框数据-秘密编号并计算不工作,请帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!