If a double is cast to an int the entire fractional part is discarded and the truncated whole number is returned.
So,
Code: Select all
double x = 2.7;
int k = (int)x;
But, is it at all possible that an integer stored as a double could have a precision/rounding error such, for example, that 3 is actually stored as 2.99999999999999999, and if so would casting this to an int return 2 rather than 3?
We were always taught to avoid equality conditions with doubles for just this reason I think. For example, never write:
Code: Select all
double x = 1.;
double y = SomeFunction(x)
if(y == 10.1)
{
// Do Something Clever.
}