How can i change this code to double.TryParse? Without try/catch.
static double PromptForDouble(string promptMessage)
{
Console.WriteLine(promptMessage);
while (true)
{
try
{
var input = Console.ReadLine(); // input = "12124124"
return double.Parse(input); // 1.234
}
catch
{
Console.WriteLine("Zła wartość");
}
}
}
}
This does it.
var input = "122.3";
if (double.TryParse(input, out var parsedInput))
{
Console.WriteLine($"success {parsedInput}");
}
else
{
Console.WriteLine($"not able to parse {input}");
}