How could I always round up a double
to an int
, and never round it down.
I know of Math.round(double)
, but I want it to always round up.
So if it was 3.2
, it gets rounded to 4.
How could I always round up a double
to an int
, and never round it down.
I know of Math.round(double)
, but I want it to always round up.
So if it was 3.2
, it gets rounded to 4.
You can use Math.ceil()
method.
See JavaDoc link: https://docs.oracle.com/javase/10/docs/api/java/lang/Math.html#ceil(double)
From the docs:
ceil
public static double ceil(double a)
Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer. Special cases:
Note that the value of Math.ceil(x) is exactly the value of -Math.floor(-x).
Parameters:
Returns:
The smallest (closest to negative infinity) floating-point value that is greater than or equal to the argument and is equal to a mathematical integer.