Convert String to Double - VB

前端 未结 5 1680
长发绾君心
长发绾君心 2020-12-01 23:41

Is there an efficient method in VB to check if a string can be converted to a double?

I\'m currently doing this by trying to convert the string to a double and then

相关标签:
5条回答
  • 2020-12-01 23:58
    Dim text As String = "123.45"
    Dim value As Double
    If Double.TryParse(text, value) Then
        ' text is convertible to Double, and value contains the Double value now
    Else
        ' Cannot convert text to Double
    End If
    
    0 讨论(0)
  • 2020-12-01 23:59

    The international versions:

        Public Shared Function GetDouble(ByVal doublestring As String) As Double
            Dim retval As Double
            Dim sep As String = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator
    
            Double.TryParse(Replace(Replace(doublestring, ".", sep), ",", sep), retval)
            Return retval
        End Function
    
        ' NULLABLE VERSION:
        Public Shared Function GetDoubleNullable(ByVal doublestring As String) As Double?
            Dim retval As Double
            Dim sep As String = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator
    
            If Double.TryParse(Replace(Replace(doublestring, ".", sep), ",", sep), retval) Then
                Return retval
            Else
                Return Nothing
            End If
        End Function
    

    Results:

            ' HUNGARIAN REGIONAL SETTINGS (NumberDecimalSeparator: ,)
    
            ' Clean Double.TryParse
            ' -------------------------------------------------
            Double.TryParse("1.12", d1)     ' Type: DOUBLE     Value: d1 = 0.0
            Double.TryParse("1,12", d2)     ' Type: DOUBLE     Value: d2 = 1.12
            Double.TryParse("abcd", d3)     ' Type: DOUBLE     Value: d3 = 0.0
    
            ' GetDouble() method
            ' -------------------------------------------------
            d1 = GetDouble("1.12")          ' Type: DOUBLE     Value: d1 = 1.12
            d2 = GetDouble("1,12")          ' Type: DOUBLE     Value: d2 = 1.12
            d3 = GetDouble("abcd")          ' Type: DOUBLE     Value: d3 = 0.0
    
            ' Nullable version - GetDoubleNullable() method
            ' -------------------------------------------------
            d1n = GetDoubleNullable("1.12") ' Type: DOUBLE?    Value: d1n = 1.12
            d2n = GetDoubleNullable("1,12") ' Type: DOUBLE?    Value: d2n = 1.12
            d3n = GetDoubleNullable("abcd") ' Type: DOUBLE?    Value: d3n = Nothing
    
    0 讨论(0)
  • 2020-12-02 00:03

    Try looking at Double.TryParse() if you are using .NET 1.1/2.0/3.0/3.5/4.0/4.5

    0 讨论(0)
  • 2020-12-02 00:04

    I simple used Eval(string) and it evaluated as Double.

    0 讨论(0)
  • 2020-12-02 00:07

    VB.NET Sample Code

    Dim A as String = "5.3"
    Dim B as Double
    
    B = CDbl(Val(A)) '// Val do hard work
    
    '// Get output 
    MsgBox (B) '// Output is 5,3 Without Val result is 53.0
    
    0 讨论(0)
提交回复
热议问题