java/코드 리뷰

12. lang2 - (3) NumberDemo2 (BigInteger 사용)

Astaroth아스 2020. 4. 23. 19:40
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package lang2;
 
import java.math.BigInteger;
 
public class NumberDemo2 {
    public static void main(String[] args) {
        
        // BigInteger : long으로 다룰 수 없는 정수
        // BigDecimal : double로 다룰 수 없는 실수
        
//        long number1 = 123456789012345678901234567890L;
//        long number2 = 1234567890L;
 
        // 정수가 long의 범위를 벗어날 때 BigInteger 사용
        BigInteger big1 = new BigInteger("123456789012345678901234567890");
        BigInteger big2 = new BigInteger("1234567890");
        
        BigInteger result1 = big1.add(big2);
        System.out.println("덧셈 결과 : " + result1.toString());
        
        BigInteger result2 = big1.subtract(big2);
        System.out.println("뺄셈 결과 : " + result2.toString());
        
        BigInteger result3 = big1.multiply(big2);
        System.out.println("곱셈 결과 : " + result3.toString());
        
        BigInteger result4 = big1.divide(big2);
        System.out.println("나눗셈 결과 : " + result4.toString());
        
    }
}