本文整理汇总了VB.NET中System.Decimal.Parse方法的典型用法代码示例。如果您正苦于以下问题:VB.NET Decimal.Parse方法的具体用法?VB.NET Decimal.Parse怎么用?VB.NET Decimal.Parse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Decimal
的用法示例。
在下文中一共展示了Decimal.Parse方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: CultureInfo
Dim value As String
Dim number As Decimal
Dim style As NumberStyles
Dim provider As CultureInfo
' Parse string using "." as the thousands separator
' and " " as the decimal separator.
value = "892 694,12"
style = NumberStyles.AllowDecimalPoint Or NumberStyles.AllowThousands
provider = New CultureInfo("fr-FR")
number = Decimal.Parse(value, style, provider)
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays:
' 892 694,12' converted to 892694.12.
Try
number = Decimal.Parse(value, style, CultureInfo.InvariantCulture)
Console.WriteLine("'{0}' converted to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}'.", value)
End Try
' Displays:
' Unable to parse '892 694,12'.
' Parse string using "$" as the currency symbol for en-GB and
' en-us cultures.
value = "$6,032.51"
style = NumberStyles.Number Or NumberStyles.AllowCurrencySymbol
provider = New CultureInfo("en-GB")
Try
number = Decimal.Parse(value, style, provider)
Console.WriteLine("'{0}' converted to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}'.", value)
End Try
' Displays:
' Unable to parse '$6,032.51'.
provider = New CultureInfo("en-US")
number = Decimal.Parse(value, style, provider)
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays:
' '$6,032.51' converted to 6032.51.
示例2: CultureInfo
Protected Sub OkToDecimal_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OkToDecimal.Click
Dim locale As String
Dim culture As CultureInfo
Dim number As Decimal
' Return if string is empty
If String.IsNullOrEmpty(Me.inputNumber.Text) Then Exit Sub
' Get locale of web request to determine possible format of number
If Request.UserLanguages.Length = 0 Then Exit Sub
locale = Request.UserLanguages(0)
If String.IsNullOrEmpty(locale) Then Exit Sub
' Instantiate CultureInfo object for the user's locale
culture = New CultureInfo(locale)
' Convert user input from a string to a number
Try
number = Decimal.Parse(Me.inputNumber.Text, culture.NumberFormat)
Catch ex As FormatException
Exit Sub
Catch ex As Exception
Exit Sub
End Try
' Output number to label on web form
Me.outputNumber.Text = "Number is " & number.ToString()
End Sub
示例3:
Dim value As String
Dim number As Decimal
' Parse an integer with thousands separators.
value = "16,523,421"
number = Decimal.Parse(value)
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays:
' 16,523,421' converted to 16523421.
' Parse a floating point value with thousands separators
value = "25,162.1378"
number = Decimal.Parse(value)
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays:
' 25,162.1378' converted to 25162.1378.
' Parse a floating point number with US currency symbol.
value = "$16,321,421.75"
Try
number = Decimal.Parse(value)
Console.WriteLine("'{0}' converted to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}'.", value)
End Try
' Displays:
' Unable to parse '$16,321,421.75'.
' Parse a number in exponential notation
value = "1.62345e-02"
Try
number = Decimal.Parse(value)
Console.WriteLine("'{0}' converted to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}'.", value)
End Try
' Displays:
' Unable to parse '1.62345e-02'.
示例4:
Dim value As String
Dim number As Decimal
Dim style As NumberStyles
' Parse string with a floating point value using NumberStyles.None.
value = "8694.12"
style = NumberStyles.None
Try
number = Decimal.Parse(value, style)
Console.WriteLine("'{0}' converted to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}'.", value)
End Try
' Displays:
' Unable to parse '8694.12'.
' Parse string with a floating point value and allow decimal point.
style = NumberStyles.AllowDecimalPoint
number = Decimal.Parse(value, style)
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays:
' '8694.12' converted to 8694.12.
' Parse string with negative value in parentheses
value = "(1,789.34)"
style = NumberStyles.AllowDecimalPoint Or NumberStyles.AllowThousands Or _
NumberStyles.AllowParentheses
number = Decimal.Parse(value, style)
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays:
' '(1,789.34)' converted to -1789.34.
' Parse string using Number style
value = " -17,623.49 "
style = NumberStyles.Number
number = Decimal.Parse(value, style)
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays:
' ' -17,623.49 ' converted to -17623.49.