Int32.Parse(String, NumberStyles, IFormatProvider) : NumberStyles « Data Types « C# / C Sharp






Int32.Parse(String, NumberStyles, IFormatProvider)

  

using System;
using System.Globalization;

public class ParseInt32
{
   public static void Main()
   {
      Convert("1,000", NumberStyles.Float | NumberStyles.AllowThousands, new CultureInfo("en-GB"));
      Convert("1,000", NumberStyles.Float | NumberStyles.AllowThousands,new CultureInfo("fr-FR"));
      Convert("1,000", NumberStyles.Float, new CultureInfo("en-US"));

      Convert("1 425,00", NumberStyles.Float | NumberStyles.AllowThousands,new CultureInfo("sv-SE"));
      Convert("1,425.00", NumberStyles.Float | NumberStyles.AllowThousands,NumberFormatInfo.InvariantInfo);
      Convert("6,900", NumberStyles.Integer | NumberStyles.AllowDecimalPoint, new CultureInfo("fr-FR"));
      Convert("6,900", NumberStyles.Integer | NumberStyles.AllowDecimalPoint,new CultureInfo("en-US"));
      Convert("6,900", 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);   
      }
   }                               
}

   
    
  








Related examples in the same category

1.Parse string to integer and NumberStyles
2.Int32.Parse (String, NumberStyles)
3.Int32.TryParse Method (String, NumberStyles, IFormatProvider, Int32)
4.Integer NumberStyles.None
5.Integer NumberStyles.AllowLeadingSign
6.NumberStyles.AllowLeadingSign | NumberStyles.AllowTrailingSign
7.NumberStyles.Integer
8.NumberStyles.Integer | NumberStyles.AllowCurrencySymbol
9.NumberStyles.Integer | NumberStyles.AllowThousands
10.NumberStyles.Integer | NumberStyles.AllowExponent
11.Parse with NumberStyles.HexNumber
12.Parse currency value using en-GB culture.
13.NumberStyles.Integer | NumberStyles.AllowDecimalPoint
14.NumberStyles.Integer | NumberStyles.AllowLeadingSign | NumberStyles.AllowTrailingSign
15.NumberStyles.HexNumber