PDA

Click to See Complete Forum and Search --> : Console.ReadLine() --> double variable


wossname
Nov 27th, 2003, 03:16 AM
How do I get the user's number into the double?


string temp;
double myDBL;

Console.Write("Enter a number: ");
temp = Console.ReadLine(); //something like "231.44324"
//perform some tests on the string to make sure its ok
//...

myDBL= temp; //causes compiler cast error


Error: Cannot convert type 'string' to 'double'

Sgt-Peppa
Nov 27th, 2003, 04:48 AM
Here you go:


myDBL= Double.Parse(temp);


That should work.
HTH,

Stephan

wossname
Nov 27th, 2003, 07:48 AM
OK thanks, is that the normal way to do it?

TheManWhoCan
Nov 27th, 2003, 12:57 PM
Don't bother with the checking part, just cast it and catch exceptions.