-
[Java] BigInteger 다루기Java 2022. 10. 2. 17:46
자바에서는 보통 int와 long을 사용하여 정수를 표현한다.
하지만 int와 long으로 표현할 수 있는 값의 크기에는 한계가 있다.
이를 해결하기 위해서 자바에서는 BigInteger 클래스를 사용한다.
BigInteger 선언
BigInteger big = new BigInteger("100"); BigInteger big2 = new BigInteger("2");
BigInteger 사칙연산
System.out.println("더하기: " + big.add(big2)); System.out.println("빼기: " + big.subtract(big2)); System.out.println("나누기: " + big.divide(big2)); System.out.println("곱하기: " + big.multiply(big2));
BigInteger 두 수 비교
int cp = big.compareTo(big2); // big이 big2보다 크면 1, 같으면 0, 작으면 -1 System.out.println("두 수 비교: " + cp);
BigInteger 형 변환
int bigInt = big.intValue(); // BigInteger -> int long bigLong = big.longValue(); // BigInteger -> long float bigFloat = big.floatValue(); // BigInteger -> float double bigDouble = big.doubleValue(); // BigInteger -> double String bigString = big.toString(); // BigInteger -> String
728x90'Java' 카테고리의 다른 글
[Java] Comparator (1) 2023.12.03 [Java] Map 성적관리 프로그램 (0) 2022.09.19 [Java] Set 성적관리 프로그램 (0) 2022.09.19 [Java] ArrayList 성적관리 프로그램 (0) 2022.09.19 [Java] 참조 자료형 (0) 2022.07.09