Mega Code Archive

 
Categories / C# / Data Types
 

Int32 Parse (String, NumberStyles)

using System; using System.Globalization; public class ParseInt32 {    public static void Main()    {       Convert("1.0", NumberStyles.AllowDecimalPoint);       Convert(" $1,234,567.89", NumberStyles.AllowCurrencySymbol |NumberStyles.Number);       Convert("1E06", 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);          }    } }