I need to cast a double to an int in Java, but the numerical value must always round down. i.e. 99.99999999 -> 99
Any ideas? :D
I need to cast a double to an int in Java, but the numerical value must always round down. i.e. 99.99999999 -> 99
Any ideas? :D
Math.floor(n)
where n is a double. This'll actually return a double, it seems, so make sure that you typecast it after.
(int)99.99999
Will be 99. Casting a double to an int does not round, it'll discard the fraction part.
Casting to an int implicitly drops any decimal part. No need to call Math.floor().
Simply typecast with (int), e.g.:
System.out.println((int)(99.9999)); // Returns 99