And if you only work with positive numbers, you can also use int i = (int) (d + 0.5).
EDIT: if you want to round negative numbers up (to positive infinity, for example, -5.4 becomes -5, for example), you can also use this. If you want to round to a higher value (rounding from -5.4 to -6), it would be useful for you to use a different function expressed by a different answer.
source share