Java: Math Class Methods
The math class is very useful in java, I'm just going to give you a little walkthrough today.
First of all we are going to cover rounding up and rounding down.
To round up in java you need to use the ceil(ing) method, for example lets say we have 3.1, the ceil method will round that to 4, as it always rounds up.
For example this:
System.out.println(Math.ceil(24.2));
Would output 25.
Rounding down is a lot like rounding up, but instead of the ceiling we want the floor, so we use the floor method. For example the following would output 1:
System.out.println(Math.floor(1.99));
Another useful math method is max this accepts two parameters, and finds the biggest of the two. For example this would output 42.6:
System.out.println(Math.max(42.6, 40));
We can also use min to find the minimum number, for example this would output 40:
System.out.println(Math.min(42.6, 40));
We can also get numbers to the power of other numbers by using pow for example if we wanted 5 to the power of 4 we would use:
System.out.println(Math.pow(5, 4));
The last one I want to go through is the square root (or sqrt). It gets the square root of a number. For example the following would output three:
System.out.println(Math.sqrt(9));
Back to Java

