import java.math.BigDecimal;

public class Main {
    public static void main(String[] args) {
        BigDecimal decimalA = new BigDecimal("20");
        BigDecimal decimalB = new BigDecimal("10");
        
        decimalA = decimalA.add(decimalB);
        System.out.println("decimalA = " + decimalA);    //더하기

        decimalA = decimalA.multiply(decimalB);
        System.out.println("decimalA = " + decimalA);    // 곱하기

        decimalA = decimalA.subtract(decimalB);
        System.out.println("decimalA = " + decimalA);    // 빼기

        decimalA = decimalA.divide(decimalB);
        System.out.println("decimalA = " + decimalA);    // 나누기

        decimalA = decimalA.pow(2);
        System.out.println("decimalA = " + decimalA);    // 제곱

        decimalA = decimalA.negate();
        System.out.println("decimalA = " + decimalA);    // 부호 변경
    }
}

 

실행결과 >

decimalA = 30
decimalA = 300
decimalA = 290
decimalA = 29
decimalA = 841
decimalA = -841

 

 

출처 : http://www.java2s.com/Code/Java/Data-Type/DomathoperationforBigDecimal.htm

 

 

+ Recent posts