250x250
반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- list multiplication
- searched case expression
- 비절차적 데이터 조작어
- MYSQL
- dense rank
- 정보처리기사
- python
- git
- 뷰
- coalesce
- 자료구조
- JavaScript
- 정규식 연산
- 위코드
- execute immediate
- SQLD
- 기업 협업
- ROLLUP
- SQL
- GROUPING
- dom
- window 함수
- show graph characteristics
- 코드 스니펫
- html
- Node.js
- Oracle
- sql 저장 모듈
- simple case expression
- 문법 차이
Archives
- Today
- Total
프로그래밍 숲
자바 JAVA BigDecimal로 단리 계산하기 본문
728x90
반응형
SimpleInterestCalculator.java
package com.in28minutes.primitive.datatype;
import java.math.BigDecimal;
public class SimpleInterestCalculator {
BigDecimal principal;
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) {
// total value = principal + principal * interest * noOfYears;
BigDecimal noOfYearsBigDecimal = new BigDecimal(noOfYears);
BigDecimal totalValue = principal.add(principal.multiply(interest).multiply(noOfYearsBigDecimal));
return totalValue;
}
}
SimpleInterestCalculatorRunner.java
package com.in28minutes.primitive.datatype;
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);
}
}
자바의 BigDecimal로 단리를 계산해 보았습니다.
파이썬으로 하다가 자바로 소숫점을 계산하는 걸 보니 아주 요상한 느낌이 드는 그런 코드입니다.
728x90
반응형
'프로그래밍_인포 > Java&JSP' 카테고리의 다른 글
자바(Java)/JSP Controller단 샅샅이 뜯어보기 - 입문자용 (0) | 2023.06.22 |
---|---|
JAVA 정수 자료형 및 거듭제곱 숫자 쉽게 복사하기 (0) | 2023.06.14 |
M1 mac에 Apache Tomcat 설치하기 (0) | 2023.06.08 |
Comments