Your current code should be working as expected and if it isn't, it's likely the result of cultural issues that expect your double string to be in a different format (i.e. 12,25
instead of 12.25
).
Applying the Current Culture When Parsing
You can handle this issue by applying the current culture when parsing your value within the Convert.ToDouble()
method through the System.Globalization
namespace:
// This would use the current culture to handle reading your value
double input = Convert.ToDouble(Console.ReadLine(),CultureInfo.InvariantCulture);
Handling Various Cultures (i.e. periods or commas)
If you wanted to expect to handle either periods or commas, you could potentially perform a replacement the in the same manner :
// Get the number separator for this culture and replace any others with it
var separator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
// Replace any periods or commas with the current culture separator and parse
var input = Double.Parse(Regex.Replace(Console.ReadLine(),"[.,]",separator));
You can see a working example of this here.