将字符串转换为double类型无效

时间:2018-03-12 14:21:22

标签: .net vb.net

我需要一些VB.Net代码的帮助,我无法通过 每次我运行它都会得到消息

  

字符串的转换对双重

无效

你能帮帮我吗?

    Dim num1 As Double = 0
    Dim num2 As Double = 0
    Dim operacion As Decimal
    ' Asignar n valores a las variables declaradas
    num1 = TextBox1.Text
    num2 = TextBox2.Text
    ' Operacion de incremento porcentual
    operacion = num1 + num1 * num2 / 100
    ' Mostrar el resultado de la operacion mediante un MsgBox - Se redondea el resultado
    MsgBox(Math.Round(operacion, 2), MsgBoxStyle.OkOnly, "Incremento Porcentual")
End Sub

1 个答案:

答案 0 :(得分:0)

显然,Option Strict off你可以这样做!

Pikoh的评论可能是正确的,你没有传递一个可以转换为Double的字符串。

If Double.TryParse(TextBox1.Text, num1) AndAlso Double.TryParse(TextBox2.Text, num2) Then
  operacion = num1 + num1 * num2 / 100
  MsgBox(Math.Round(operacion, 2), MsgBoxStyle.OkOnly, "Incremento Porcentual")
Else
  MsgBox("Please enter valid numbers")
End If