Mega Code Archive

 
Categories / C# Book / 02 Essential Types
 

0317 Using NumberStyles

NumberStyles allows to control how to parse a string for number. NumberStyles has the following formats: AllowCurrencySymbol AllowDecimalPoint AllowExponent AllowHexSpecifier AllowLeadingSign AllowLeadingWhite AllowParentheses AllowThousands AllowTrailingSign AllowTrailingWhite NumberStyles also defines these composite members: Any Currency Float HexNumber Integer None Number using System; using System.Text; using System.Globalization; class Sample { public static void Main() { int thousand = int.Parse("13E8", NumberStyles.HexNumber); int minusTwo = int.Parse("(112)", NumberStyles.Integer | NumberStyles.AllowParentheses); double aMillion = double.Parse("1,000,000", NumberStyles.Any); Console.WriteLine(aMillion); decimal threeMillion = decimal.Parse("3e6", NumberStyles.Any); Console.WriteLine(threeMillion); decimal fivePointTwo = decimal.Parse("$5.20", NumberStyles.Currency); Console.WriteLine(fivePointTwo); } } The output: 1000000 3000000 5.20