Math
- Math.round()
BigDecimal
java.math.BigDecimal
- BigDecimal (int)
- BigDecimal (double)
- BigDecimal (long)
- BigDecimal (String) // better
- Methods
- add (value)
- compareTo(value)
- divide(value, scale, rounding-mode)
- multiply(value)
- setScale(scale, rounding-mode)
- subtract(value)
- toString()
java.math.RoundingMode
- HALF_UP
- HALF_EVEN
import java.math.*; // imports all classes and // enumerations in java.math // convert subtotal and discount percent to BigDecimal BigDecimal decimalSubtotal = new BigDecimal(Double.toString(subtotal)); decimalSubtotal = decimalSubtotal.setScale(2, RoundingMode.HALF_UP); BigDecimal decimalDiscountPercent = new BigDecimal(Double.toString(discountPercent)); // calculate discount amount BigDecimal discountAmount = decimalSubtotal.multiply(decimalDiscountPercent); discountAmount = discountAmount.setScale( 2, RoundingMode.HALF_UP); // calculate total before tax, sales tax, and total BigDecimal totalBeforeTax = decimalSubtotal.subtract(discountAmount); BigDecimal salesTaxPercent = new BigDecimal(SALES_TAX_PCT); BigDecimal salesTax = salesTaxPercent.multiply(totalBeforeTax); salesTax = salesTax.setScale(2, RoundingMode.HALF_UP); BigDecimal total = totalBeforeTax.add(salesTax); // Create another BigDecimal object from existing BigDecimal Object BigDecimal total2 = new BigDecimal(total.toString());
No comments:
Post a Comment