作者:VB.NET开发
项目:Syste
' 导入命名空间
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
作者:VB.NET开发
项目:Syste
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
作者:VB.NET开发
项目:Syste
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
作者:VB.NET开发
项目:Syste
' 导入命名空间
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