본문 바로가기
Today I Learned/Java

[Java] 절대값 구하기 Math.abs( )

by 프로그래 밍구 2023. 11. 22.

Math.abs( )

int, long, double, float 타입을 입력받아 절대값을 반환한다.

 

public class Main {
    public static void main(String[] args) {

        int num1 = -3;
        long num2 = -534978333;
        double num3 = -3.235;
        float num4 = -1.234f;

        System.out.println("Math.abs(num1) = " + Math.abs(num1));
        System.out.println("Math.abs(num2) = " + Math.abs(num2));
        System.out.println("Math.abs(num3) = " + Math.abs(num3));
        System.out.println("Math.abs(num4) = " + Math.abs(num4));
    }
}

// Math.abs(num1) = 3
// Math.abs(num2) = 534978333
// Math.abs(num3) = 3.235
// Math.abs(num4) = 1.234

 

댓글