Search code examples
c#inputconsole.readline

C# ReadLine float


I try convert string to float because I using Console.ReadLine() for input.

The Console.ReadLine() only accept string values, but I need convert. How I can do that?

Thank you.


Solution

  •         float val = float.Parse(Console.ReadLine());
            Console.WriteLine(val);
    

    OR

            float val2;
            if (!float.TryParse(Console.ReadLine(), out val2))
            {
                Console.WriteLine("Not a valid float");
            }
            else {
                Console.WriteLine(val2);
            }