자바의 기본 클래스 'Math' - Math static method
Math 클래스의 static 메서드
자바에는 간단하게 연산을 도와주는 Math 클래스의 static method가 존재한다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class MathMain {
public static void main(String[] args) {
double db1 = 59.53123, db2 = 86.23543;
System.out.println("그냥 출력 "+db1+" "+db2);
//반올림하기
System.out.println("그냥 반올림 "+Math.round(db1)+" "+ Math.round(db2));
//원하는 특정한 곳에서 반올림
System.out.println("특정한곳 반올림 "+(Math.round(db1*100))/100d+" "+ Math.round(db2*100)/100d);
//올림 기냥 무지성으로 올려버림
System.out.println("\n올림 "+Math.ceil(db1)+" "+ Math.ceil(db2));
//내림 그냥 소수점 버림
System.out.println("내림 "+Math.floor(db1)+" "+ Math.floor(db2));
//절대값
int a = 20, b = -30;
System.out.println(a+" "+b);
System.out.println("\n절대 값 "+Math.abs(a)+" "+Math.abs(b));
}
}
| 메서드 | 예제 |
|---|---|
Math.ceil(double a) | Math.ceil(4.3) -> 5.0 |
Math.floor(double a) | Math.floor(4.9) -> 4.0 |
Math.round(float a) | Math.round(4.5) -> 5 |
Math.round(double a) | Math.round(4.5) -> 5L |
Math.abs(int a) | Math.abs(-10) -> 10 |
Math.abs(double a) | Math.abs(-10.5) -> 10.5 |
이 작성글은 저작권자의 CC BY 4.0 라이센스를 따릅니다.

