Mega Code Archive

 
Categories / C# / Data Types
 

Parse a string in exponential notation with the AllowExponent and Number flags

using System; using System.Globalization; using System.Threading; public class ParseString {     public static void Main()     {         Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");         string value = "123.123";         NumberStyles styles;         styles = NumberStyles.AllowExponent | NumberStyles.Number;         ShowNumericValue(value, styles);     }     private static void ShowNumericValue(string value, NumberStyles styles)     {         Single number;         try         {             number = Single.Parse(value, styles);             Console.WriteLine("Converted '{0}' using {1} to {2}.", value, styles.ToString(), number);         }         catch (FormatException)         {             Console.WriteLine("Unable to parse '{0}' with styles {1}.", value, styles.ToString());         }     } }