I tried this expression 12/13 in c# and I got value as 1 When I cast the value to double then actual value is coming.
Can someone explain me why this is happening?
Thanks Ajit
I tried this expression 12/13 in c# and I got value as 1 When I cast the value to double then actual value is coming.
Can someone explain me why this is happening?
Thanks Ajit
It is because of Integer Division. Because both of your values are Integers, the result will be an integer as well.
You could do a *1.0 to force a double whenever you do a calculation, and it also doesnt change the solution.
The value of 12/13
is smaller than 1
, which means that due to integer trunctaion it will truncated to 0
.
Any operation with integer results answer in integer. any operation with double results answer in double. That is default to change scenario you need to cast so that you can get result per your need.