자바에서 절대값을 주는 함수를 사용하고 싶다면 Math.abs(절대값을 원하는 파라미터)를 사용하면 된다.
👉Math.abs()에 대한 자바 공식문서 링크
공식문서에 의하면 절대값을 원하는 파라미터는
int, long, float, double 타입이 가능하다.
아래는 예시 코드다.
public class Math_abs {
public static void main(String[] args) {
int intAbs = Math.abs(-10);
System.out.println(intAbs); // 10
long longAbs = Math.abs(-10);
System.out.println(longAbs); // 10
double doubleAbs = Math.abs(-9.9);
System.out.println(doubleAbs); // 9.9
float floatAbs = Math.abs(-9.9f);
System.out.println(floatAbs); // 9.9
}
}
아래는 실행 결과다.