Converts string in a specified style and culture-specific format to its 32-bit signed integer equivalent.
Imports System.Globalization
Module ParseInt32
Public Sub Main()
Convert("999,000", NumberStyles.Float Or NumberStyles.AllowThousands, New CultureInfo("en-GB"))
Convert("999,000", NumberStyles.Float Or NumberStyles.AllowThousands, New CultureInfo("fr-FR"))
Convert("999,000", NumberStyles.Float, New CultureInfo("en-US"))
Convert("999 999,00", NumberStyles.Float Or NumberStyles.AllowThousands, New CultureInfo("sv-SE"))
Convert("999,999.00", NumberStyles.Float Or NumberStyles.AllowThousands, NumberFormatInfo.InvariantInfo)
Convert("999,900", NumberStyles.Integer Or NumberStyles.AllowDecimalPoint, New CultureInfo("fr-FR"))
Convert("999,900", NumberStyles.Integer Or NumberStyles.AllowDecimalPoint, New CultureInfo("en-US"))
Convert("999,900", NumberStyles.Integer Or NumberStyles.AllowThousands, New CultureInfo("en-US"))
End Sub
Private Sub Convert(value As String, style As NumberStyles,provider As IFormatProvider)
Try
Dim number As Integer = Int32.Parse(value, style, provider)
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to convert '{0}'.", value)
Catch e As OverflowException
Console.WriteLine("'{0}' is out of range of the Int32 type.", value)
End Try
End Sub
End Module
Related examples in the same category