NumberStyles and DateTimeStyles

NumberStyles and DateTimeStyles control how parsing works.


using System;
using System.Text;
using System.Globalization;
class Sample
{
    public static void Main()
    {
        int error = int.Parse("(2)");  // Exception thrown

        int minusTwo = int.Parse("(2)", NumberStyles.Integer | NumberStyles.AllowParentheses);  // OK

        decimal fivePointTwo = decimal.Parse("$5.20", NumberStyles.Currency, CultureInfo.GetCultureInfo("en-GB"));

    }
}

The output:

 
Unhandled Exception: System.FormatException: Input string was not in a correct f
ormat.
   at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffe
r& number, NumberFormatInfo info, Boolean parseDecimal)
   at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo in
fo)
   at Sample.Main()
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.