Mesuko Mesuko - 3 months ago 13
C# Question

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.

Answer
        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);
        }
Comments