How to declare double as a variable with Console.Readline() | SoloLearn: Learn to code for FREE!


How to declare double as a variable with Console.Readline()

Hello. My question is how to declare a variable in double format.I am trying System.Convert.ToDouble but i have error "Input string was not in a correct format"... My Code: double a = System.Convert.ToDouble(Console.ReadLine());


6/1/2020 4:19:04 PM


6 Answers

New Answer


Mine appears to work fine.


Matthew Having read your previous reply, probably your computer system was setup with a locale which uses comma ',' instead of a dot '.' for decimal separator. If this was the case, then follow the settings and use comma as decimal separator for giving input.


If you want to add a bit of fault tolerance to the code, you can use `TryParse` instead. You get to check whether the given input was convertible to `double` type by that. // paste in main method double d; bool okay = Double.TryParse(Console.ReadLine(), out d); if(okay) // conversion success! { Console.WriteLine("The double value is {0}", d); } else // something's not right { Console.WriteLine("Your input is not okay."); }


What was your input string?


"3.12" for example


I use Visual studio and if i put 3.12 its error but if i use comma 3,12 its ok... It is fine or what?