본문 바로가기

프로그래밍 언어/Java

[Java] Math.abs() 함수

Math.abs() 함수


abs() 함수는 인자값에 대한 절대값을 반환하는 함수로, 인자값의 타입은 int, float, long, double이 있다.

인자값에 따라 4개로 구분되고, static 함수이기 때문에 접근이 용이하다.




Math.abs() 함수 원형 


1
2
3
4
5
6
7
static double abs( double d )
 
static float abs( float f )
 
static int abs( int i )
 
static long abs( long l )
cs







<Math.abs() 함수 사용 예시>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class t {
    public static void main(String[] args) {
        int i = -10;
        System.out.println(Math.abs(i));
 
        float f = -15.2f;
        System.out.println(Math.abs(f));
 
        long l = -20;
        System.out.println(Math.abs(l));
 
        double d = -25.3;
        System.out.println(Math.abs(d));
    }
}
cs




<실행 결과>


10

15.2

20

25.3