0

DIFFERENT TYPE

is there any other way we can add two numbers of different type? like 5+5.5. Becaus i thought template was actually useful for that purpose.

21st May 2020, 8:35 PM
TSERING LOWA
TSERING LOWA - avatar
1 Antwort
+ 4
You don't need template for that, you can calculate with two different number types, just like you wrote. if you use + with int and double, the result will also be a double. If you want to store that value, you just have to watch out that the container can fit in that double. So if you write... int n = 5+5.5; ... the result will be a double, but because you press it in an int container, the decimals will fall right off again. So you need to write: double d = 5+5.5; The other problem you could have is something like this: double d = 5/2; You expect to get 2.5, but instead you get 2.0. That's because *both* the operands are int. So 5/2 is 2, and if you press that in a double, it becomes 2.0.
21st May 2020, 8:46 PM
HonFu
HonFu - avatar