메뉴 건너뛰기

조회 수 3713 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

엄청 큰 숫자형의 연산은 BigDecimal 형으로 변경 후 계산을 하면 좋습니다.

 

import java.math.BigDecimal;

public class Test {

 public static void main(String[] args) {
  BigDecimal bdcl1 = new BigDecimal("12345678901234567899");
  BigDecimal bdcl2 = new BigDecimal("12345678901234567890");
  
  // 더하기
  BigDecimal p_add = bdcl1.add(bdcl2);
  // 빼기
  BigDecimal p_sub = bdcl1.subtract(bdcl2);
  // 곱하기
  BigDecimal p_mul = bdcl1.multiply(bdcl2);
  // 나누기 - 무조건 반올림
  BigDecimal p_div1 = bdcl1.divide(bdcl2, BigDecimal.ROUND_UP);
  // 나누기 - 소수점 4번째 자리에서 반올림.
  BigDecimal p_div2 = bdcl1.divide(bdcl2, 4, BigDecimal.ROUND_UP);
  
  System.out.println("덧셈 : " + p_add);
  System.out.println("뺄셈 : " + p_sub);
  System.out.println("곱셈 : " + p_mul);
  System.out.println("나눗셈1 : " + p_div1);
  System.out.println("나눗셈2 : " + p_div2);

 }
}

 

=================================================================================

 

덧셈 : 24691357802469135789
뺄셈 : 9
곱셈 : 152415787532388367613016309986130163110
나눗셈1 : 2
나눗셈2 : 1.0001

List of Articles
번호 제목 날짜 조회 수
111 public static void main(String [] args) 2016.09.13 3143
110 Reflection을 활용한 메서드, 필드 값 불러오기. 2021.03.31 122
109 request header 로부터 접속 정보 확인 file 2023.02.15 75
108 Select statements cannot be executed as an update. 에러 해결방법 file 2016.08.29 3846
107 set get 파라미터 2016.08.18 3658
106 Singleton Pattern 과 DeadLock file 2023.02.15 74
105 Spring Boot 프로젝트 생성 file 2016.09.02 4160
104 static 2016.09.13 3354
103 static멤버와 관련된 예제 2016.09.21 3768
102 System.out.println(); 이클립스에서 자동화기능 사용 file 2016.09.19 5157
101 TCP 소켓 프로그래밍 01 - Server/Client 일대일 연결 file 2021.03.31 119
100 war로 묶지 않아도 컴파일된 소스 위치 확인하기 file 2016.08.29 4116
99 XML to JSON , JSON to Map 2020.06.29 262
98 [객체 지향 언어의 이해] 업캐스팅과 다운캐스팅 file 2021.03.31 157
97 [자바(스프링&mybatis&jsp) 프로젝트 & 아파치 &톰켓 연동 ] 이클립스 프로젝트 생성 순서04.jdbc 드라이버 설치 file 2016.08.18 4209
96 [자바(스프링&mybatis&jsp) 프로젝트 & 아파치 &톰켓 연동 ] 이클립스 프로젝트 생성 순서07.commons-collection 설치 file 2016.08.18 3845
95 [자바(스프링&mybatis&jsp) 프로젝트 & 아파치 &톰켓 연동 ] 이클립스 프로젝트 생성 순서08.commons-logging 설치 file 2016.08.18 4458
94 [자바(스프링&mybatis&jsp) 프로젝트 & 아파치 &톰켓 연동 ]11. 이클립스 프로젝트 생성 file 2016.08.18 3972
93 [자바(스프링&mybatis&jsp) 프로젝트 & 아파치 &톰켓 연동 ]9. 이클립스 압타나 플러그인 설치 file 2016.08.18 4012
92 [자바(스프링&mybatis&jsp) 프로젝트 & 아파치 &톰켓 연동]3. 이클립스 설치 file 2016.08.18 3756
Board Pagination Prev 1 2 3 4 5 6 7 8 Next
/ 8

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved