Mega Code Archive

 
Categories / VB.Net / Data Types
 

Single Parse Method converts string in a style and culture-specific format to single-precision floating-point number

Imports System.Globalization Module Example     Public Sub Main()       Dim values() As String = { " 987.654E-2", " 987,654E-2", _                                  "9.876.543,210",  "98_76_54_32,19" }       Dim ci As New CultureInfo("")       ci.NumberFormat.NumberGroupSizes = New Integer() { 2 }       ci.NumberFormat.NumberGroupSeparator = "_"       Dim providers() As CultureInfo = { New CultureInfo("en-US"),New CultureInfo("nl-NL"), ci }              Dim styles() As NumberStyles = { NumberStyles.Currency, NumberStyles.Float }       For Each provider As CultureInfo In providers          Console.WriteLine("Parsing using the {0} culture:", If(provider.Name = String.Empty, "Invariant", provider.Name))          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