BAEL-2154 - BigDecimal and BigInteger article.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user