Tags
- postman html parse
- Interaction developer
- postman collection variables
- Java
- web developer
- oracle
- MFC
- 우수한 프런트 개발자
- emplace_back
- Unity
- LSL_Script
- postman automations
- postman collection
- 좋은 개발자
- postman session
- solidity
- Android
- postman
- postman csv
- Front-end developer
- Android/iOS Developer
- c#
- Intellij
- C++
- postman tests
- 다빈치 리졸브
- postman excel
- 프런트엔드
- postman pre-request
- UI/UX Engineer
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Archives
- Today
- Total
david's daily developer note
[BE] Java BigDecimal 본문
728x90
BigDecimal은 java 언어에서 부동소수점의 정밀한 계산을 할때 사용된다.
다음은 BigDecimal 사용할때 유용한 내용을 요약 정리한 글이다.
1. ArithmeticException
표현하고 있는 자릿수보다 작은 자릿수로 변경하려고 하면 종종 발생
new BigDecimal(123.123456).setScale(3);//java.lang.ArithmeticException: Rounding necessary
new BigDecimal(123.123456).setScale(3, RoundingMode.HALF_UP);//OK
2. Compare
BigDecimal의 CompareTo 함수는 직관적이지 않다. 사람이 이해하기 쉬운 방식으로 표현하고 결함을 줄이자.
public boolean graterThen(BigDecimal first, BigDecimal second) {
return first.compareTo(second) > 0;
}
public boolean graterOrEqual(BigDecimal first, BigDecimal second) {
return first.compareTo(second) >= 0;
}
public boolean lessThen(BigDecimal first, BigDecimal second) {
return first.compareTo(second) < 0;
}
public boolean lessOrEqualTo(BigDecimal first, BigDecimal second) {
return first.compareTo(second) <= 0;
}
public boolean equalTo(BigDecimal first, BigDecimal second) {
return first.compareTo(second) == 0;
}
3. Sum using Stream
BigDecimal 값의 합계와 나누기 연산
List<BigDecimal> values = new LinkedList<>();
values.add(BigDecimal.valueOf(1.0));
values.add(BigDecimal.valueOf(2.0));
values.stream()
.reduce(BigDecimal.ZERO, BigDecimal::add)
.divide(BigDecimal.valueOf(1.0), 0/*scale*/, RoundingMode.HALF_UP);
728x90
'[Develop] Web > Back-end' 카테고리의 다른 글
[BE] 코드 품질 추적을 위한 SonarLint (0) | 2022.07.18 |
---|---|
[BE] Java Enum Class Example (0) | 2022.07.07 |
[BE] Spring boot Excel Download (feat. apache.poi) (0) | 2022.07.05 |
[BE] Spring @Controller vs @RestController (0) | 2022.07.05 |
[BE] Naver IntelliJ formatter Setting (Code Style) (0) | 2022.06.30 |