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

0

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());

c#

6/1/2020 4:19:04 PM

Matthew

6 Answers

New Answer

+2

Mine appears to work fine. https://code.sololearn.com/cv7VbRRFRCE1/?ref=app

+2

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.

+1

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."); }

0

What was your input string?

0

"3.12" for example

0

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?