【问题标题】:In VB.NET evaluate 0.70 to be equal to .7在 VB.NET 中评估 0.70 等于 .7
【发布时间】:2014-12-11 18:47:10
【问题描述】:
我正在比较用户在 DataGridView 单元格中输入的值 - 来自编辑控件的该值将以字符串开头 - 与来自数据源的十进制值(定义为十进制(3,2)) .
如何评估用户输入的值“.7”,例如,等于数据库值 0.70?
【问题讨论】:
标签:
vb.net
datagridview
sqldatasource
comparison-operators
【解决方案1】:
您可以使用CDec 函数将字符串值转换为小数。例如
If CDec(".7") = 0.7 Then
' This will be true
End If
如果您不确定用户输入的值是否为有效的小数,那么您应该使用Decimal.TryParse:
Dim value As Decimal = 0
If Decimal.TryParse(".7", value) Then
If value = 0.7 Then
' This will be true
End If
End If
【解决方案2】:
Dim str As String = ".7"
Dim test As Double = Double.Parse(str)
MessageBox.Show(test)
您也可以使用 Plutonix 指示的 Decimal.Parse。