问题描述
大家好,我试图在这里进行计算.但是当我输入0时,其值为Nan?如何将其替换为0. 我尝试过的事情:
hello guys im trying to make calculation here . but when i input 0 its values is Nan ? how to replace it by 0 instead. What I have tried:
totalms = Val(wwms1.Text) + Val(wwms2.Text) + Val(wwms3.Text) + Val(wwms4.Text) + Val(wwms5.Text) + Val(wwms6.Text) + Val(wwms7.Text) + Val(wwms8.Text) + Val(wwms9.Text) + Val(wwms10.Text)
totalrs = Val(wwrs1.Text) + Val(wwrs2.Text) + Val(wwrs3.Text) + Val(wwrs4.Text) + Val(wwrs5.Text) + Val(wwrs6.Text) + Val(wwrs7.Text) + Val(wwrs8.Text) + Val(wwrs9.Text) + Val(wwrs10.Text)
推荐答案
使用 ^ ]确实是一种不好的做法.请点击链接以查找原因. 我建议使用正确的方法将文本转换为数字.例如: VB:
CDbl()
,CLng()
等,请参见:类型转换函数 [ ^ ]
VB.NET:
Double.TryParse方法(字符串,NumberStyles,IFormatProvider,Double)(系统) [ ^ ]
Double.Parse方法(字符串)(系统) [ ^ ]
用伪代码:
Well, using Val function[^] is really bad practice. Please, follow the link to find out why.
I''d suggest to use a proper way to convert text into numbers. For example:
VB:
CDbl()
, CLng()
, etc., See: Type Conversion Functions[^]
VB.NET:
Double.TryParse Method (String, NumberStyles, IFormatProvider, Double) (System)[^]
Double.Parse Method (String) (System)[^]
In pseudo code:
Dim tot As Double, tmp As Double
On Error GoTo Err_Txt2Dbl
For Each obj As TextBox In Form1.Controls
If obj.Name Like "wwms*" Then
tmp = 0
'if conversion is possible, calculate total
tmp = CDbl(obj.Text)
tot += tmp
End If
Next
Exit_MySubroutine:
Exit Sub
Err_Txt2Dbl:
Select Case Err.Number
Case 6 'overflow
tmp = 0
Resume Next
End Select
Dim st As NumberStyles = NumberStyles.Number Or NumberStyles.AllowCurrencySymbol
Dim ci As CultureInfo = CultureInfo.CreateSpecificCulture("en-us")
Dim tot As Double = 0
Dim tmp As Double = 0
For Each obj As TextBox In Form1.Controls
If obj.Name.StartsWith("wwms") Then
tmp = 0
'if conversion is possible, calculate total
If Double.TryParse(control.Text, st, ci, tmp) Then
tot += tmp
End If
End If
Next
有关更多详细信息,请参见: 关于错误声明(Visual Basic) [ ^ ]
For further details, please see: On Error Statement (Visual Basic)[^] Error Handling in Visual Basic .NET[^]