//BigInteger
타입 | 범위 |
int | -2,147,483,648 ~ 2,147,483,647 |
long | -9,223,372,036,854,775,808 ~9,223,372,036,854,775,807 |
BigInteger | 무한(∞) |
- 숫자가 지나치게 커서 int나 long의 범위를 벗어날 땐 BigInteger를 사용할 수 있다.
- import java.math.BigInteger;
- 생성 및 선언
BigInteger 변수명 = new BigInteger(원하는 숫자-문자열의 형태로);
BigInteger num1 = new BigInteger("10");
※ BigInteger를 생성할 때 생성자 안에 꼭 문자열의 형태로 숫자를 입력해야 한다.
- 더하기
num1.add(num2);
- 빼기
num1.subtract(num2);
- 곱하기
num1.multiply(num2);
- 나누기
num1.divide(num2);
- 최대공약수
num1.gcd(num2);
- 결과를 int로 변환
num.intValue();
import java.math.BigInteger;
public class Ex16 {
public static void main(String[] args) {
BigInteger num1 = new BigInteger("10");
BigInteger num2 = new BigInteger("5");
System.out.println("10 + 5 = " + num1.add(num2));
System.out.println("10 - 5 = " + num1.subtract(num2));
System.out.println("10 * 5 = " + num1.multiply(num2));
System.out.println("10 / 2 = " + num1.divide(num2) + "\n");
BigInteger num3 = new BigInteger("24");
num3 = num3.add(new BigInteger("6"));
System.out.println("24 + 6 = " + num3);
}
}
import java.math.BigInteger;
public class Ex26 {
public static void main(String[] args) {
int a = 12;
int b = 21;
int answer = 1;
BigInteger numerator = BigInteger.valueOf(a);
BigInteger denominator = BigInteger.valueOf(b);
BigInteger gcd_ = numerator.gcd(denominator);
int gcd = gcd_.intValue();
System.out.println(gcd);
}
}
** BigDecimal 클래스를 이용해서 소수도 활용할 수 있다.
package com.sorrel012.primitive.datatypes;
import java.math.BigDecimal;
public class SimpleInterestCalculator {
private BigDecimal principal;
private BigDecimal interest;
public SimpleInterestCalculator(String principal, String interest) {
this.principal = new BigDecimal(principal);
this.interest = new BigDecimal(interest).divide(new BigDecimal(100));
}
public BigDecimal calculateTotalValue(int noOfYears) {
BigDecimal totalValue = principal.add(principal.multiply(interest).multiply(new BigDecimal(noOfYears)));
return totalValue;
}
}
package com.sorrel012.primitive.datatypes;
import java.math.BigDecimal;
public class SimpleInterestCalculatorRunner {
public static void main(String[] args) {
SimpleInterestCalculator calculator = new SimpleInterestCalculator("4500.00", "7.5");
BigDecimal totalValue = calculator.calculateTotalValue(5);
System.out.println(totalValue);
}
}
package com.sorreo012.arrays;
import java.math.BigDecimal;
public class StudentRunner {
public static void main(String[] args) {
int[] marks = {97, 98, 100};
Student student = new Student("Hanee", marks);
int number = student.getNumberOfMarks();
int sum = student.getTotalSumOfMarks();
int maxMark = student.getMaxMark();
int minMark = student.getMinMark();
BigDecimal average = student.getAverageMarks();
System.out.println("number of marks : " + number);
System.out.println("sum of marks : " + sum);
System.out.println("maximum of marks : " + maxMark);
System.out.println("minimum of marks : " + minMark);
System.out.println("average of marks : " + average);
} //main
}
package com.sorreo012.arrays;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class Student {
private String name;
private int[] marks;
public Student(String name, int[] marks) {
this.name = name;
this.marks = marks;
}
public int getNumberOfMarks() {
return marks.length;
} //getNumberOfMarks
public int getTotalSumOfMarks() {
int sum = 0;
for(int mark : marks) {
sum += mark;
}
return sum;
} //getTotalSumOfMarks
public int getMaxMark() {
int max = Integer.MIN_VALUE;
for(int mark : marks) {
if(mark > max) {
max = mark;
}
}
return max;
} //getMaxMark
public int getMinMark() {
int min = Integer.MAX_VALUE;
for(int mark : marks) {
if(mark < min) {
min = mark;
}
}
return min;
} //getMinMark
public BigDecimal getAverageMarks() {
int sum = getTotalSumOfMarks();
int number = getNumberOfMarks();
return new BigDecimal(sum).divide(new BigDecimal(number), 3, RoundingMode.UP);
}
'자바(JAVA)' 카테고리의 다른 글
[자바(Java)] 파일과 디렉토리 (0) | 2023.01.24 |
---|---|
☆이클립스 개인 설정 (0) | 2023.01.14 |
[자바(Java)] 10진수 <-> 2진수 변환 (0) | 2023.01.02 |
[자바(Java)] char <-> int 변환 (0) | 2022.12.31 |
[자바(Java)] 스트림 활용 (0) | 2022.12.31 |