C# Int32 Parse(String, NumberStyles)
Description
Int32 Parse(String, NumberStyles)
converts the string
representation of a number in a specified style to its 32-bit signed integer
equivalent.
Syntax
Int32.Parse(String, NumberStyles)
has the following syntax.
public static int Parse(
string s,
NumberStyles style
)
Parameters
Int32.Parse(String, NumberStyles)
has the following parameters.
s
- A string containing a number to convert.style
- A bitwise combination of the enumeration values that indicates the style elements that can be present in s. A typical value to specify is Integer.
Returns
Int32.Parse(String, NumberStyles)
method returns A 32-bit signed integer equivalent to the number specified in s.
Example
The following example uses the Int32.Parse(String, NumberStyles) method to parse the string representations of several Int32 values.
using System;/* w w w .j av a 2 s.com*/
using System.Globalization;
public class ParseInt32
{
public static void Main()
{
Convert("14.0", NumberStyles.AllowDecimalPoint);
Convert("14.9", NumberStyles.AllowDecimalPoint);
Convert(" $12,345,678.92", NumberStyles.AllowCurrencySymbol |
NumberStyles.Number);
Convert("103E06", NumberStyles.AllowExponent);
Convert("-1,234,567", NumberStyles.AllowThousands);
Convert("(1,234,567)", NumberStyles.AllowThousands |
NumberStyles.AllowParentheses);
}
private static void Convert(string value, NumberStyles style)
{
try
{
int number = Int32.Parse(value, style);
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.