DEV_Beginner
DEV_Beginner

Reputation: 41

Why do I need to convert console input to a specific data type?

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

Upvotes: 0

Views: 135

Answers (2)

Carter
Carter

Reputation: 2870

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.

Upvotes: 2

FlySwat
FlySwat

Reputation: 175653

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.

Upvotes: 2

Related Questions