2009-07-23 35 views
17

Có phương pháp hiệu quả trong VB để kiểm tra xem một chuỗi có thể được chuyển đổi thành một đôi không?Chuyển đổi chuỗi thành đôi - VB

Tôi hiện đang làm điều này bằng cách cố gắng chuyển đổi chuỗi thành gấp đôi và sau đó xem liệu nó có ném ngoại lệ hay không. Nhưng điều này dường như làm chậm ứng dụng của tôi.

Try 
    ' if number then format it. 
    current = CDbl(x) 
    current = Math.Round(current, d) 
    Return current 
Catch ex As System.InvalidCastException 
    ' item is not a number, do not format... leave as a string 
    Return x 
End Try 
+0

vb.net i giả? –

Trả lời

20

Hãy thử nhìn vào Double.TryParse() nếu bạn đang sử dụng NET 1,1/2,0/3,0/3,5/4,0/4,5

+0

Không giống như các phương pháp TryParse khác, Double.TryParse đã có khoảng ít nhất là .NET 1.1 http://msdn.microsoft.com/en-us/library/system.double.tryparse%28v=vs.71%29.aspx –

11
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 
16

VB.NET Mẫu Mã

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 
+1

Điều này làm việc chuyển đổi một chuỗi để tăng gấp đôi. Tôi đã thử Convert.ToDouble, cdbl và Double.TryParse. Nhưng phải mất một Val() để thực sự làm công việc. – bendecko

3

Phiên bản quốc tế:

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 

Kết quả:

 ' 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 
Các vấn đề liên quan