예제 코드public class MathExample {
public static void main(String[] args) {
// 절대값 구하기
int absoluteValue = Math.abs(-5);
System.out.println("Absolute Value: " + absoluteValue);
// 최대값, 최소값 구하기
int maxNum = Math.max(10, 20);
int minNum = Math.min(30, 40);
System.out.println("Max: " + maxNum + ", Min: " + minNum);
// 제곱근 구하기
double sqrtValue = Math.sqrt(25);
System.out.println("Square Root: " + sqrtValue);
// 랜덤값 생성
double randomValue = Math.random(); // 0.0에서 1.0 사이의 난수
System.out.println("Random Value: " + randomValue);
// 반올림, 올림, 내림
double roundValue = Math.round(3.14);
double ceilValue = Math.ceil(3.14);
double floorValue = Math.floor(3.14);
System.out.println("Round: " + roundValue + ", Ceil: " + ceilValue + ", Floor: " + floorValue);
}
}
실행 결과
Absolute Value: 5
Max: 20, Min: 30
Square Root: 5.0
Random Value: 0.5965551523933841
Round: 3.0, Ceil: 4.0, Floor: 3.0