Source Code : Calculation with BigDecimal
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
Calculation with BigDecimal
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
public class BigDecimalInvoiceApp {
public static void main(String[] args) {
double subtotal = 123.123;
double discountPercent = 0.2;
BigDecimal decimalSubtotal = new BigDecimal(Double.toString(subtotal));
decimalSubtotal = decimalSubtotal.setScale(2, RoundingMode.HALF_UP);
BigDecimal decimalDiscountPercent = new BigDecimal(Double.toString(discountPercent));
BigDecimal discountAmount = decimalSubtotal.multiply(decimalDiscountPercent);
discountAmount = discountAmount.setScale(2, RoundingMode.HALF_UP);
BigDecimal totalBeforeTax = decimalSubtotal.subtract(discountAmount);
BigDecimal salesTaxPercent = new BigDecimal(".05");
BigDecimal salesTax = salesTaxPercent.multiply(totalBeforeTax);
salesTax = salesTax.setScale(2, RoundingMode.HALF_UP);
BigDecimal total = totalBeforeTax.add(salesTax);
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
String message = "Subtotal: " + currency.format(decimalSubtotal) + "
"
+ "Discount percent: " + percent.format(decimalDiscountPercent) + "
"
+ "Discount amount: " + currency.format(discountAmount) + "
" + "Total before tax: "
+ currency.format(totalBeforeTax) + "
" + "Sales tax: " + currency.format(salesTax)
+ "
" + "Invoice total: " + currency.format(total) + "
";
System.out.println(message);
}
}
/*Subtotal: $123.12
Discount percent: 20%
Discount amount: $24.62
Total before tax: $98.50
Sales tax: $4.93
Invoice total: $103.43
*/
Thank with us