views:

83

answers:

2

Looking for more help on converting to doubles, integers, and decimal format when doing calculations.

EX: ...Console.Write(" INPUT TOTAL SALES : ");

...userInput = Console.ReadLine();

...totalSales = Convert.ToDouble(userInput);

I'm not completely understanding why I needed to convert such to a double, why it couldn't just be Console.ReadLine();

TY sorry if this is so amateur. LOL

+2  A: 

LOL indeed.

So the reason you need to convert to double is because Console.ReadLine reads in a string.

I'd recommend you read:

http://msdn.microsoft.com/en-us/library/cs7y5x0x.aspx so you can grasp the different datatypes.

FlySwat
Thank you I appreciate it.
Brent
+2  A: 

User input is a string, not a double (or any number in the way you want it to be for that manner). So two different data types here.

If you wanted to do a calculation like userInput + 5 or something for whatever reason, if userInput is a string it's going to either flip out or give you unexpected results depending on the compiler/language.

Perhaps it will help to think of it this way. If you didn't convert to a double first it would be like trying to do this...

"2.33" + 5

You might as well be doing this...

"HEY!" + 5

once it's converted though it's more like this...

2.33 + 5

Notice no more quotes implying 2.33 is now a number not a string.

Carter
That's almost perfectly said for me. I appreciate that very much.
Brent