當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET Decimal.Parse方法代碼示例

本文整理匯總了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.
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:45,代碼來源:Decimal.Parse

示例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
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:28,代碼來源:Decimal.Parse

示例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'.
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:38,代碼來源:Decimal.Parse

示例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.
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:39,代碼來源:Decimal.Parse


注:本文中的System.Decimal.Parse方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。