当前位置: 首页>>代码示例>>VB.NET>>正文


VB.NET Single.Parse方法代码示例

本文整理汇总了VB.NET中System.Single.Parse方法的典型用法代码示例。如果您正苦于以下问题:VB.NET Single.Parse方法的具体用法?VB.NET Single.Parse怎么用?VB.NET Single.Parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Single的用法示例。


在下文中一共展示了Single.Parse方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。

示例1: Example

' 导入命名空间
Imports System.Globalization

Module Example
    Public Sub Main()
      ' Define an array of string values.
      Dim values() As String = { " 987.654E-2", " 987,654E-2", _
                                 "(98765,43210)", "9,876,543.210",  _
                                 "9.876.543,210",  "98_76_54_32,19" }
      ' Create a custom culture based on the invariant culture.
      Dim ci As New CultureInfo("")
      ci.NumberFormat.NumberGroupSizes = New Integer() { 2 }
      ci.NumberFormat.NumberGroupSeparator = "_"
      
      ' Define an array of format providers.
      Dim providers() As CultureInfo = { New CultureInfo("en-US"), _
                                             New CultureInfo("nl-NL"), ci }       
      
      ' Define an array of styles.
      Dim styles() As NumberStyles = { NumberStyles.Currency, NumberStyles.Float }
      
      ' Iterate the array of format providers.
      For Each provider As CultureInfo In providers
         Console.WriteLine("Parsing using the {0} culture:", _
                           If(provider.Name = String.Empty, "Invariant", provider.Name))
         ' Parse each element in the array of string values.
         For Each value As String In values
            For Each style As NumberStyles In styles
               Try
                  Dim number As Single = Single.Parse(value, style, provider)            
                  Console.WriteLine("   {0} ({1}) -> {2}", _
                                    value, style, number)
               Catch e As FormatException
                  Console.WriteLine("   '{0}' is invalid using {1}.", value, style)            
               Catch e As OverflowException
                  Console.WriteLine("   '{0}' is out of the range of a Single.", value)
               End Try 
            Next            
         Next         
         Console.WriteLine()
      Next
   End Sub   
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:43,代码来源:Single.Parse

输出:

Parsing using the en-US culture:
987.654E-2' is invalid using Currency.
987.654E-2 (Float) -> 9.87654
987,654E-2' is invalid using Currency.
987,654E-2' is invalid using Float.
(98765,43210) (Currency) -> -9.876543E+09
(98765,43210)' is invalid using Float.
9,876,543.210 (Currency) -> 9876543
9,876,543.210' is invalid using Float.
9.876.543,210' is invalid using Currency.
9.876.543,210' is invalid using Float.
98_76_54_32,19' is invalid using Currency.
98_76_54_32,19' is invalid using Float.

Parsing using the nl-NL culture:
987.654E-2' is invalid using Currency.
987.654E-2' is invalid using Float.
987,654E-2' is invalid using Currency.
987,654E-2 (Float) -> 9.87654
(98765,43210) (Currency) -> -98765.43
(98765,43210)' is invalid using Float.
9,876,543.210' is invalid using Currency.
9,876,543.210' is invalid using Float.
9.876.543,210 (Currency) -> 9876543
9.876.543,210' is invalid using Float.
98_76_54_32,19' is invalid using Currency.
98_76_54_32,19' is invalid using Float.

Parsing using the Invariant culture:
987.654E-2' is invalid using Currency.
987.654E-2 (Float) -> 9.87654
987,654E-2' is invalid using Currency.
987,654E-2' is invalid using Float.
(98765,43210) (Currency) -> -9.876543E+09
(98765,43210)' is invalid using Float.
9,876,543.210 (Currency) -> 9876543
9,876,543.210' is invalid using Float.
9.876.543,210' is invalid using Currency.
9.876.543,210' is invalid using Float.
98_76_54_32,19 (Currency) -> 9.876543E+09
98_76_54_32,19' is invalid using Float.

示例2: CultureInfo

Protected Sub OkToSingle_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OkToSingle.Click
   Dim locale As String
   Dim culture As CultureInfo
   Dim number As Single

   ' 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 = Single.Parse(Me.inputNumber.Text, culture.NumberFormat)
   Catch ex As FormatException
      Exit Sub
   Catch ex As OverflowException
      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,代码来源:Single.Parse

示例3: Example

Module Example
   Public Sub Main()
      Dim values() As String = { "100", "(100)", "-123,456,789", "123.45e+6", _
                                 "+500", "5e2", "3.1416", "600.", "-.123", _
                                 "-Infinity", "-1E-16", Double.MaxValue.ToString(), _
                                 Single.MinValue.ToString(), String.Empty }
      For Each value As String In values
         Try   
            Dim number As Single = Single.Parse(value)
            Console.WriteLine("{0} -> {1}", value, number)
         Catch e As FormatException
            Console.WriteLine("'{0}' is not in a valid format.", value)
         Catch e As OverflowException
            Console.WriteLine("{0} is outside the range of a Single.", value)
         End Try
      Next                                  
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:18,代码来源:Single.Parse

输出:

100 -> 100
(100)' is not in a valid format.
-123,456,789 -> -1.234568E+08
123.45e+6 -> 1.2345E+08
+500 -> 500
5e2 -> 500
3.1416 -> 3.1416
600. -> 600
-.123 -> -0.123
-Infinity -> -Infinity
-1E-16 -> -1E-16
1.79769313486232E+308 is outside the range of a Single.
-3.402823E+38 -> -3.402823E+38
is not in a valid format.

示例4: ParseStrings

' 导入命名空间
Imports System.Globalization
Imports System.Threading

Module ParseStrings
   Public Sub Main()
      ' Set current thread culture to en-US.
      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US")
            
      Dim value As String
      Dim styles As NumberStyles
      
      ' Parse a string in exponential notation with only the AllowExponent flag. 
      value = "-1.063E-02"
      styles = NumberStyles.AllowExponent
      ShowNumericValue(value, styles) 
      
      ' Parse a string in exponential notation
      ' with the AllowExponent and Number flags.
      styles = NumberStyles.AllowExponent Or NumberStyles.Number
      ShowNumericValue(value, styles)

      ' Parse a currency value with leading and trailing white space, and
      ' white space after the U.S. currency symbol.
      value = " $ 6,164.3299  "
      styles = NumberStyles.Number Or NumberStyles.AllowCurrencySymbol
      ShowNumericValue(value, styles)
      
      ' Parse negative value with thousands separator and decimal.
      value = "(4,320.64)"
      styles = NumberStyles.AllowParentheses Or NumberStyles.AllowTrailingSign _
               Or NumberStyles.Float 
      ShowNumericValue(value, styles)
      
      styles = NumberStyles.AllowParentheses Or NumberStyles.AllowTrailingSign _
               Or NumberStyles.Float Or NumberStyles.AllowThousands
      ShowNumericValue(value, styles)
   End Sub
   
   Private Sub ShowNumericValue(value As String, styles As NumberStyles)
      Dim number As Single
      Try
         number = Single.Parse(value, styles)
         Console.WriteLine("Converted '{0}' using {1} to {2}.", _
                           value, styles.ToString(), number)
      Catch e As FormatException
         Console.WriteLine("Unable to parse '{0}' with styles {1}.", _
                           value, styles.ToString())
      End Try
      Console.WriteLine()                           
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:52,代码来源:Single.Parse

输出:

Unable to parse '-1.063E-02' with styles AllowExponent.

Converted '-1.063E-02' using AllowTrailingSign, AllowThousands, Float to -0.01063.

Converted ' $ 6,164.3299  ' using Number, AllowCurrencySymbol to 6164.3299.

Unable to parse '(4,320.64)' with styles AllowTrailingSign, AllowParentheses, Float.

Converted '(4,320.64)' using AllowTrailingSign, AllowParentheses, AllowThousands, Float to -4320.64.


注:本文中的System.Single.Parse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。