本文整理匯總了VB.NET中System.Double.TryParse方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET Double.TryParse方法的具體用法?VB.NET Double.TryParse怎麽用?VB.NET Double.TryParse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Double
的用法示例。
在下文中一共展示了Double.TryParse方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: Example
Module Example
Public Sub Main()
Dim values() As String = { "1,643.57", "$1,643.57", "-1.643e6",
"-168934617882109132", "123AE6",
Nothing, String.Empty, "ABCDEF" }
Dim number As Double
For Each value In values
If Double.TryParse(value, number) Then
Console.WriteLine("'{0}' --> {1}", value, number)
Else
Console.WriteLine("Unable to parse '{0}'.", value)
End If
Next
End Sub
End Module
輸出:
1,643.57' --> 1643.57 Unable to parse '$1,643.57'. -1.643e6' --> -1643000 -168934617882109132' --> -1.68934617882109E+17 Unable to parse '123AE6'. Unable to parse ''. Unable to parse ''. Unable to parse 'ABCDEF'.
示例2: Example
Module Example
Public Sub Main()
Dim value As String
Dim number As Double
value = Double.MinValue.ToString()
If Double.TryParse(value, number) Then
Console.WriteLine(number)
Else
Console.WriteLine("{0} is outside the range of a Double.", _
value)
End If
value = Double.MaxValue.ToString()
If Double.TryParse(value, number) Then
Console.WriteLine(number)
Else
Console.WriteLine("{0} is outside the range of a Double.", _
value)
End If
End Sub
End Module
輸出:
-1.79769313486232E+308 is outside the range of the Double type. 1.79769313486232E+308 is outside the range of the Double type.
示例3:
Dim value As String
Dim style As NumberStyles
Dim culture As CultureInfo
Dim number As Double
' Parse currency value using en-GB culture.
value = "£1,097.63"
style = NumberStyles.Number Or NumberStyles.AllowCurrencySymbol
culture = CultureInfo.CreateSpecificCulture("en-GB")
If Double.TryParse(value, style, culture, number) Then
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Else
Console.WriteLine("Unable to convert '{0}'.", value)
End If
' Displays:
' Converted '£1,097.63' to 1097.63.
value = "1345,978"
style = NumberStyles.AllowDecimalPoint
culture = CultureInfo.CreateSpecificCulture("fr-FR")
If Double.TryParse(value, style, culture, number) Then
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Else
Console.WriteLine("Unable to convert '{0}'.", value)
End If
' Displays:
' Converted '1345,978' to 1345.978.
value = "1.345,978"
style = NumberStyles.AllowDecimalPoint Or NumberStyles.AllowThousands
culture = CultureInfo.CreateSpecificCulture("es-ES")
If Double.TryParse(value, style, culture, number) Then
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Else
Console.WriteLine("Unable to convert '{0}'.", value)
End If
' Displays:
' Converted '1.345,978' to 1345.978.
value = "1 345,978"
If Double.TryParse(value, style, culture, number) Then
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Else
Console.WriteLine("Unable to convert '{0}'.", value)
End If
' Displays:
' Unable to convert '1 345,978'.