Mega Code Archive

 
Categories / C# / Data Types
 

Converts string to float

using System; public class Example {    public static void Main()    {       string[] values = { "(100)", "-123,456,789", "123.45e+6",                            "+500", "5e2", "3.1", "6.", "-.123",                            "-Infinity", "-1E-16", Double.MaxValue.ToString(),                            Single.MinValue.ToString(), String.Empty };       foreach (string value in values)       {          try {                float number = Single.Parse(value);             Console.WriteLine("{0} -> {1}", value, number);          }          catch (FormatException) {             Console.WriteLine("'{0}' is not in a valid format.", value);          }          catch (OverflowException) {             Console.WriteLine("{0} is outside the range of a Single.", value);          }       }                                      } }