本文整理汇总了VB.NET中System.Decimal.TryParse方法的典型用法代码示例。如果您正苦于以下问题:VB.NET Decimal.TryParse方法的具体用法?VB.NET Decimal.TryParse怎么用?VB.NET Decimal.TryParse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Decimal
的用法示例。
在下文中一共展示了Decimal.TryParse方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1:
Dim value As String
Dim style As NumberStyles
Dim culture As CultureInfo
Dim number As Decimal
' Parse currency value using en-GB culture.
value = "£1,097.63"
style = NumberStyles.Number Or NumberStyles.AllowCurrencySymbol
culture = CultureInfo.CreateSpecificCulture("en-GB")
If Decimal.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 Decimal.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 Decimal.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 Decimal.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'.
示例2:
Dim value As String
Dim number As Decimal
' Parse a floating-point value with a thousands separator.
value = "1,643.57"
If Decimal.TryParse(value, number) Then
Console.WriteLine(number)
Else
Console.WriteLine("Unable to parse '{0}'.", value)
End If
' Parse a floating-point value with a currency symbol and a
' thousands separator.
value = "$1,643.57"
If Decimal.TryParse(value, number) Then
Console.WriteLine(number)
Else
Console.WriteLine("Unable to parse '{0}'.", value)
End If
' Parse value in exponential notation.
value = "-1.643e6"
If Decimal.TryParse(value, number)
Console.WriteLine(number)
Else
Console.WriteLine("Unable to parse '{0}'.", value)
End If
' Parse a negative integer value.
value = "-1689346178821"
If Decimal.TryParse(value, number)
Console.WriteLine(number)
Else
Console.WriteLine("Unable to parse '{0}'.", value)
End If
输出:
1643.57 Unable to parse '$1,643.57'. Unable to parse '-1.643e6'. -1689346178821