회사에서 특정 케이스에 할인율이 맞지 않는 경우가 버그 건으로 제보가 들어왔습니다.
그래서 확인 해보니 할일율을 계산하는 로직에 버그가 있었습니다.
원래 로직은 아래와 같습니다.
int getDiscountRatio(int originPrice, int sellPrice) {
return Math.floorDiv((originPrice - sellPrice)*100, originPrice);
}
문제는 originPrice - sellPrice에 100을 곱한 것이 Integer 범위를 넘어 설 때였습니다.
Math 클래스를 사용하지 않은 사칙 연산은 타입의 범위를 넘더라도 예외를 발생시키지 않는데요.
모르는 버그보다는 예외가 나으므로 다음부터는 Math 클래스를 적극 사용하기로 했습니다.
또 현재 코드로는 버그를 해결할 수 없으므로 먼저 비율을 구하고 100을 곱하는 형태로 코드를 수정하였습니다.
int getDiscountRatio(int originPrice, int sellPrice) {
var discountAmount = Math.subtractExact(originPrice, sellPrice);
var discountRatio = discountAmount * 100D / originPrice;
return (int) discountRatio;
}
'트러블슈팅' 카테고리의 다른 글
[Java, Thread] InheritableThreadLocal, ThreadPool 이슈 (0) | 2023.12.17 |
---|---|
[Spring] Spock 테스트 패키지 문제 - IntelliJ 빌드 설정 (0) | 2023.11.19 |
[Docker] window heredocs 줄바꿈 이슈 (0) | 2023.09.10 |
[JPA] QueryHint 버전별 차이 해소 (0) | 2023.08.06 |
[JPA] @Version 어노테이션 동작 (0) | 2023.07.29 |