Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
We'll learn about how to make integers and doubles play nice with each other.
Builtin C# Types
Mixing integers and doubles in arithmetic operations:
Left Side of Operator  Right Side of Operator  Message 

int  double  double 
int  int  int (beware of truncation!) 
double  int  double 
Start REPL: csharp
Quit REPL: quit

0:00
Now that we have another data type that can store numbers

0:02
let's take a look at how they work together.

0:04
Remember when we tried to add an integer to a string and

0:07
we just ended up with a longer string?

0:09
That happened because the program assumed that we wanted to treat the integer

0:14
like a string.

0:15
A similar thing happens when you attempt to do math that mixes int's and doubles.

0:19
Let's check this out in the REPL.

0:22
Let's declare an int x and a double y.

0:27
The rule is actually pretty simple, any arithmetic operation that involves both

0:31
an int and a double will always result in a double.

0:34
We can check this out by wrapping the operation in parentheses, and

0:38
then calling the GetType method on the result.

0:41
This does the addition and then shows us the name of the data type of the result.

0:45
As you can see, the result is of type system.double.

0:49
It says system.double instead of double

0:52
because that is the actual name of the double type.

0:55
Double is actually just an alias or pseudonym for system.double.

1:00
And you can use them interchangeably.

1:03
The actual name of int is system.int32.

1:05
I've included a link to a list of the built in C Sharp types, and

1:10
their names, in the teacher's notes.

1:12
Programmers almost always use the short versions, int and double.

1:17
As you can see, the rest of these operations also result in a double.

1:29
Even if the result is a whole number,

1:31
the resulting data type will still be a double.

1:33
5 divided by 2.5 is 2, right?

1:37
Let's see what happens when you to assign that to an int.

1:41
We get an error saying that we can't convert a double to an end and

1:45
then mentions a cast.

1:47
Casts are use to convert between data types.

1:50
To cast this to an integer, we just surround it with parenthesis and

1:54
then type int in parenthesis in front of it.

1:57
As you can see the int Z now contains 2.

2:01
Let's try doing another cast what do you think will happen when we do this?

2:08
Let's take a look at X.

2:11
X is now 2.

2:13
You might have expected 2.9 to be rounded to the nearest whole number

2:16
when it was cast to an int.

2:18
In which case, x would have been three.

2:21
Instead, when a decimal number is cast to an integer,

2:24
everything after the decimal point is removed.

2:27
This is called truncation.

2:29
Don't get it confused with rounding.

2:31
Truncation also happens when doing division with two integers.

2:36
What will happen with we divide 7 by 3?

2:40
We get 2.

2:40
If you do this operation on a calculator, you'll get two and a third.

2:45
So what happened to the onethird?

2:48
Well, 7 and 3 are both integers.

2:50
See..

2:51
No decimal point.

2:53
The result of an arithmetic operation between two ints is always an int,

2:57
even if the result isn't a whole number.

3:01
What if we were to assign 7 divided by 3 to a double?

3:04
Maybe that will work?

3:06
Let's inspect the value of y now.

3:09
y is still 2.

3:10
You see, C# evaluates everything on the right side of the equals sign first,

3:16
and then assigns it to the variable.

3:18
Since 7 and 3 are both integers, the result is an integer and

3:23
the decimal value is truncated.

3:25
So we end up with 2 in y.

3:28
If we wanted to end up with 2 and

3:29
a third, we'd need to cast one of these numbers to a double.

3:33
The other way we could do this is just to make one of these numbers a double

3:37
by appending a point zero to it.

3:39
In more complex mathematical operations we need to pay very close attention to

3:43
the order of the operations,

3:44
otherwise we might run into unintended truncation problems.

3:49
Let's take a look at this expression.

3:51
Let's run it again after changing the 6 to 6.0.

3:56
You see, the first operation performed in this expression is the divide, and

4:01
9 and 6 are both integers.

4:04
This results in 1.

4:06
Then, 0.3 is added.

4:08
And we end up with 1.3, instead of 1.8 as you might expect.

4:13
We'd get the same result if we were using variables instead of literal numbers.

4:19
Truncation has caused programmers a lot of grief over the years.

4:22
I've even been bitten by it myself.

4:25
There are a couple of tips you can use to avoid such problems though.

4:28
First, if you're using a literal number, and you intend the result to be a double.

4:33
You should always append .0 to the end of the number, to make your intentions clear.

4:37
Second, if you're doing division using an integer variable like this,

4:43
just make it a habit to always cast one or both of the operands to a double.

4:48
There are cases where we do want the result to be truncated.

4:51
But it's fairly rare.

4:53
Finally, I've included a small table in the Teacher's Notes

4:55
to remind you about the rules of mixing integers and doubles.
You need to sign up for Treehouse in order to download course files.
Sign up