【发布时间】:2014-02-14 09:30:24
【问题描述】:
我编写了一个相当原始的共享函数来评估输入到文本框中的数值。用户可以输入小数或整数,我评估的范围是 5.0 到 14.0。 if 语句的前两部分返回 true,但当输入整数且没有小数点时,最后一部分返回 false 的小数位数。请问如何修改此函数以在所有路径上返回true?
Public Shared Function CheckDecimalValue(ByVal txtbox As TextBox) As Boolean
Dim returnValue As Boolean = True
Dim txtAsDecimal As Decimal = CType(txtbox.Text, Decimal)
Dim decimalAsString As String = CType(txtAsDecimal, String)
Dim indexOfDecimalPoint As Integer = decimalAsString.IndexOf(".")
Dim numberOfDecimals As Integer = decimalAsString.Substring(indexOfDecimalPoint + 1).Length
If txtAsDecimal < 5 Then
returnValue = False
ElseIf txtAsDecimal > 14 Then
returnValue = False
ElseIf numberOfDecimals > 1 Then
returnValue = False
End If
Return returnValue
End Function
【问题讨论】:
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
-
为什么要检查小数位数?您可能可以删除该检查。