C# Int32 Parse(String, NumberStyles, IFormatProvider)
Description
Int32 Parse(String, NumberStyles, IFormatProvider)
converts
the string representation of a number in a specified style and culture-specific
format to its 32-bit signed integer equivalent.
Syntax
Int32.Parse(String, NumberStyles, IFormatProvider)
has the following syntax.
public static int Parse(
string s,//from ww w . j a v a 2 s. c o m
NumberStyles style,
IFormatProvider provider
)
Parameters
Int32.Parse(String, NumberStyles, IFormatProvider)
has the following parameters.
s
- A string containing a number to convert.style
- A bitwise combination of enumeration values that indicates the style elements that can be present in s. A typical value to specify is Integer.provider
- An object that supplies culture-specific information about the format of s.
Returns
Int32.Parse(String, NumberStyles, IFormatProvider)
method returns A 32-bit signed integer equivalent to the number specified in s.
Example
The following example uses a variety of style and provider parameters to parse the string representations of Int32 values.
/*from w w w . j a va 2 s. c o m*/
using System;
using System.Globalization;
public class ParseInt32
{
public static void Main()
{
Convert("12,345", NumberStyles.Float | NumberStyles.AllowThousands,
new CultureInfo("en-GB"));
Convert("12,345", NumberStyles.Float | NumberStyles.AllowThousands,
new CultureInfo("fr-FR"));
Convert("12,000", NumberStyles.Float, new CultureInfo("en-US"));
Convert("12 345,00", NumberStyles.Float | NumberStyles.AllowThousands,
new CultureInfo("sv-SE"));
Convert("12,345.00", NumberStyles.Float | NumberStyles.AllowThousands,
NumberFormatInfo.InvariantInfo);
Convert("123,456", NumberStyles.Integer | NumberStyles.AllowDecimalPoint,
new CultureInfo("fr-FR"));
Convert("123,456", NumberStyles.Integer | NumberStyles.AllowDecimalPoint,
new CultureInfo("en-US"));
Convert("123,456", NumberStyles.Integer | NumberStyles.AllowThousands,
new CultureInfo("en-US"));
}
private static void Convert(string value, NumberStyles style,
IFormatProvider provider)
{
try
{
int number = Int32.Parse(value, style, provider);
Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to convert '{0}'.", value);
}
catch (OverflowException)
{
Console.WriteLine("'{0}' is out of range of the Int32 type.", value);
}
}
}
The code above generates the following result.