- 수학관련 static메서드의 집합
- round()로 원하는 소수점 아래 세 번째 자리에서 반올림하기
1. 원래 값에 100을 곱한다.
2. 위의 결과에 Math.round()를 사용한다.
3. 위의 결과를 다시 100.0으로 나눈다
| 메서드 / 설명 | 예제 | 결과 |
| static double abs(double a) static float abs(float f) static int abs(int f) static long abs(long f) |
int i = Math.abs(-10); double d = Math.abs(-10.0); |
i = 10 d = 10.0 |
| 주어진 값의 절대값을 반환한다. | ||
| static double ceil(double a) | double d = Math.ceil(10.1); | d = 11.0 |
| 주어진 값을 올림하여 반환한다. | ||
| static double floor(double a) | double d = Math.floor(10.8); | d = 10 |
| 주어진 값을 버림하여 반환한다. | ||
| static double max(double a, double b) static float max(float a, float b) static int max(int a, int b) static long max(long a, long b) |
double d = Math.max(9.5, 9.50001); int i = Math.max(0, -1) |
d = 9.50001 i = 0 |
| 주어진 두 값을 비교하여 큰 쪽을 반환한다. |
| static double min(double a, double b) static float min(float a, float b) static int min(int a, int b) static long min(long a, long b) |
double d = Math.min(9.5, 9.50001); int i = Math.min(0, -1) |
i = 9.5 d = -1 |
| 두 값을 비교하여 작은 쪽을 반환한다. | ||
| static double random() | double d = Math.random(); int i = (int)(Math.random() * 10) + 1 |
0.0 <= d <1.0 1 <= i < 11 |
| 0.0~1.0범위의 임의의 double값을 반환한다. (1.0은 범위에 포함되지 않는다.) | ||
| static double rint(double a) | double d = Math.rint(1.2); double d2 = Math.rint(3.5); double d3 = Math.rint(4.5); |
d = 1.0 d2 = 3.5 d3 = 4.5 |
| 주어진 double값과 가장 가까운 정수값을 double형으로 반환한다. 단, 두 정수의 정가운데 있는 값(1.5, 2.5, 3.5 등)은 짝수를 반환 | ||
| static long round(double a) static long round(float a) |
double d = Math.rint(1.2); double d2 = Math.rint(3.5); double d3 = Math.rint(4.5); |
d = 1 d2 = 4 d3 = 5 |
| 소수점 첫째자리에서 반올림한 정수값(long)을 반환한다. 두 정수의 정가운데있는 값은 항상 큰 정수를 반환(rint()의 결과와 비교) |
'Programming > 자바(Java)' 카테고리의 다른 글
| 11-1 컬렉션 프레임웍 (1) | 2024.10.11 |
|---|---|
| 9-25 래퍼(wrapper)클래스 / Number클래스 (0) | 2024.10.11 |
| 9-15 StringBuffer클래스 / StringBuilder (0) | 2024.10.10 |
| 9-12, 13 join()과 StringJoiner / 문자열과 기본형 간의 변환 (0) | 2024.10.08 |
| 9-11 String클래스의 생성자와 메서드 (0) | 2024.10.08 |