BAEL-2154 - BigDecimal and BigInteger article.

This commit is contained in:
chandra
2018-09-07 01:01:17 -04:00
parent 04c255c887
commit b94147540a
3 changed files with 279 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
import java.math.BigDecimal;
import java.math.RoundingMode;
public class BigDecimalDemo {
/** Calculate total amount to be paid for an item rounded to cents..
* @param quantity
* @param unitPrice
* @param discountRate
* @param taxRate
* @return
*/
public static BigDecimal calculateTotalAmount(BigDecimal quantity,
BigDecimal unitPrice, BigDecimal discountRate, BigDecimal taxRate) {
BigDecimal amount = quantity.multiply(unitPrice);
BigDecimal discount = amount.multiply(discountRate);
BigDecimal discountedAmount = amount.subtract(discount);
BigDecimal tax = taxRate.multiply(taxRate);
BigDecimal total = discountedAmount.add(tax);
// round to 2 decimal places using HALF_EVEN
BigDecimal roundedTotal = total.setScale(2, RoundingMode.HALF_EVEN);
return roundedTotal;
}
}