转换字符串为double无效

时间:2014-08-24 07:02:23

标签: vb.net

以下代码提供从字符串到doble的错误转换无效

Dim TaxRebate = CDbl(ComboBox1.SelectedItem) * 0.01 * CDbl(Premium)

对此有所帮助表示赞赏 Sunilsb

3 个答案:

答案 0 :(得分:0)

您可能正在寻找Double.TryParse()

  

转换指定样式中数字的字符串表示形式   和文化特定的格式到它的双精度浮点   等价数字。

它自1.1版以来就存在

答案 1 :(得分:0)

'Dim TaxRebate = CDbl(ComboBox1.SelectedItem) * 0.01 * CDbl(Premium)

Dim value1 As String
Dim number1 As Double

Dim value2 As String
Dim number2 As Double

Dim TaxRebate As Double

value1 = ComboBox1.SelectedItem.ToString()
value2 = Premium.ToString()

If False = Double.TryParse(value1, number1) then
    Console.WriteLine("{0} is outside the range of a Double.", value1)
ElseIf False = Double.TryParse(value2, number2) Then
    Console.WriteLine("{0} is outside the range of a Double.", value2)
Else
    TaxRebate = 0.01 * number1 * number2 
End If

答案 2 :(得分:0)

简单

Dim TaxRebate As Double = CDbl(ComboBox1.SelectedItem) * 0.01 * CDbl(Premium)

将解决问题。

我们必须声明它们的变量/文本字符串

Dim ------正如Doule =