Skip to content
thesarfo

Concept

The Math Class

Rounding, ceiling/floor, min/max, and generating random numbers with java.lang.Math.

views 0
public class Main {
public static void main(String[] args) {
int result = Math.round(1.1F); // rounds the float to the nearest int
int result1 = (int) Math.ceil(1.1F); // returns 2
int result2 = (int) Math.floor(1.1F); // returns 1
int result3 = Math.max(1, 2); // returns the max value, i.e. 2
int result4 = Math.min(1, 2); // returns the min value, i.e. 1
double result5 = Math.random(); // returns a random value between 0 and 1 (a double)
double result6 = Math.random() * 100; // returns a random value between 0 and 100
int result7 = (int) Math.round(Math.random() * 100); // round first, then convert to int
int result8 = (int) (Math.random() * 100); // same as above but without rounding
}
}