Originally posted by bunsung92 March 7, 2023 📝 구성
Table of contents generated with markdown-toc
지양
반올림 모드
�float과 double은 부동소수를 사용한다. 금융계산처럼 명확해야 하는 계산에는 지양해야한다.
아래와 같은 코드가 있다.
private static void floatingCalculate() { double funds = 1.00; int itemBought = 0; for (double price = 0.10; funds >= price; price += 0.10) { funds -= price; itemBought++; } System.out.println(itemBought + "개 구입"); System.out.println("잔돈(달러) : " + funds); }
기대한 결과는 사탕 1개 -> 사탕 2개 -> 사탕 3개 -> 사탕 4개 의 구매를 통해 1 + 2 + 3 + 4의 결과로 1달러의 소비를 원했다.
하지만 실제 결과는
좀 더 계산이 명확해야한다.
private static void bigDecimalCalculate() { final BigDecimal TEN_CENTS = new BigDecimal(".10"); int itemBought = 0; BigDecimal funds = new BigDecimal(1.00); for (BigDecimal price = TEN_CENTS; funds.compareTo(price) >= 0; price = price.add(TEN_CENTS)) { funds = funds.subtract(price); itemBought++; } System.out.println(itemBought + "개 구입"); System.out.println("잔돈(달러) : " + funds); }
부동소수점의 사용과 달리 명확한 계산결과가 반환된다. 하지만 BigDecimal은 두가지 단점이 있다.
이를 대안할 수 있는 방법이 있는데...
위의 식을 다시 생각 해보면 다룰 수 있는 값의 크기가 제한할 수있다. (꼭 소수점을 사용하지 않아도 계산이 된다.) 즉 아래와 같은 코드로 작성이 가능하다.
private static void integerCalculate() { int itemBought = 0; int funds = 100; for (int price = 10; funds >= price; price += 10) { funds -= price; itemBought++; } System.out.println(itemBought + "개 구입"); System.out.println("잔돈(센트) : " + funds); }
결과는 아래와 같다.
int는 아홉 자리 십진수 long 열여덟 자리 십진수 열여덟 자리 십진수 이상은 BigDecimal 사용
2023.03.19 일
Activity