java.lang.Math 클래스
- Math 클래스의 모든 메소드는 클래스메소드(static 메소드) 이므로, 객체 생성하지 않고 바로 사용할 수 있다.
random()
- 0.0이상 1.0미만 범위에서 임의의 double 형 값을 하나 생성하여 반환한다.
System.out.println(Math.random()); // 0.0 ~ 0.9999999999...
System.out.println((int) (Math.random() * 4)); // 0 ~ 3
System.out.println((int) (Math.random() * 10)); // 0 ~ 9
abs()
- 전달된 값의 절댓값 반환
System.out.println(Math.abs(5)); // 10
System.out.println(Math.abs(-5)); // 10
System.out.println(Math.abs(-3.14)); // 3.14
cell() - 올림
floor() - 내림
round() - 반올림
System.out.println(Math.ceil(10.0)); // 10.0
System.out.println(Math.ceil(10.3)); // 11.0
System.out.println(Math.ceil(10.000001)); // 11.0
System.out.println(Math.floor(10.0)); // 10.0
System.out.println(Math.floor(10.9)); // 10.0
System.out.println(Math.round(10.0)); // 10
System.out.println(Math.round(10.4)); // 10
System.out.println(Math.round(10.5)); // 11
max() - 전달된 값을 비교, 그 중 큰 값 반환
min() - 전달된 값을 비교, 그 중 작은 값 반환
System.out.println(Math.max(1.23, 1.29)); // 1.29
System.out.println(Math.min(1.23, 1.29)); // 1.23
pow() - n의 m승 반환
sqrt() - n의 제곱근 반환
System.out.println(Math.pow(2, 4)); // 16.0
System.out.println(Math.sqrt(4)); // 2.0
System.out.println(Math.sqrt(2)); // 1.4142135623730951
'백엔드개발자 > JAVA' 카테고리의 다른 글
@Data , hashCode() (0) | 2022.05.16 |
---|---|
프로세스, 스레드 (0) | 2022.05.10 |
Map (0) | 2022.04.25 |
Collection(List, Set), Map (0) | 2022.04.07 |
Presentation Logic 과 Business Logic (0) | 2022.01.02 |