Example usage for java.math BigDecimal multiply

List of usage examples for java.math BigDecimal multiply

Introduction

In this page you can find the example usage for java.math BigDecimal multiply.

Prototype

public BigDecimal multiply(BigDecimal multiplicand) 

Source Link

Document

Returns a BigDecimal whose value is (this × multiplicand), and whose scale is (this.scale() + multiplicand.scale()) .

Usage

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("1.230");
    BigDecimal bg2 = new BigDecimal("4.560");

    BigDecimal bg3 = bg1.multiply(bg2);

    System.out.println(bg3);//w ww. j  a va 2s  . c om
}

From source file:BigDec.java

public static void main(String args[]) {
    BigDecimal rate = new BigDecimal(".03251234");
    BigDecimal months = new BigDecimal("12");
    BigDecimal monthlyRate = rate.divide(months, BigDecimal.ROUND_HALF_DOWN);
    System.out.println("Annual rate : " + rate);
    System.out.println("Monthly rate: " + monthlyRate);
    BigDecimal balance = new BigDecimal("10000.0000");
    for (int i = 0; i < 12; i++) {
        BigDecimal interest = balance.multiply(monthlyRate);
        balance = balance.add(interest);
        System.out.println("Balance: " + balance);
    }//from w  ww .  j  ava2 s.  c o m
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    BigDecimal bd1 = new BigDecimal("123456789.0123456890");

    // Create via a long
    BigDecimal bd2 = BigDecimal.valueOf(123L);

    bd1 = bd1.multiply(bd2);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    // Create via a string
    BigDecimal bd1 = new BigDecimal("123456789.0123456890");

    // Create via a long
    BigDecimal bd2 = BigDecimal.valueOf(123L);

    bd1 = bd1.add(bd2);//from ww  w.  j  ava2s.  com
    bd1 = bd1.multiply(bd2);
    bd1 = bd1.subtract(bd2);
    bd1 = bd1.divide(bd2, BigDecimal.ROUND_UP);
    bd1 = bd1.negate();
}

From source file:Main.java

public static void main(String[] args) {
    BigDecimal decimalA = new BigDecimal("123456789012345");
    BigDecimal decimalB = new BigDecimal("10");

    decimalA = decimalA.add(decimalB);//from ww  w . j  a  v  a 2  s  . co  m
    System.out.println("decimalA = " + decimalA);

    decimalA = decimalA.multiply(decimalB);
    System.out.println("decimalA = " + decimalA);

    decimalA = decimalA.subtract(decimalB);
    System.out.println("decimalA = " + decimalA);

    decimalA = decimalA.divide(decimalB);
    System.out.println("decimalA = " + decimalA);

    decimalA = decimalA.pow(2);
    System.out.println("decimalA = " + decimalA);

    decimalA = decimalA.negate();
    System.out.println("decimalA = " + decimalA);
}

From source file:BigDecimalInvoiceApp.java

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) + "\n" + "Discount percent: "
            + percent.format(decimalDiscountPercent) + "\n" + "Discount amount:  "
            + currency.format(discountAmount) + "\n" + "Total before tax: " + currency.format(totalBeforeTax)
            + "\n" + "Sales tax:        " + currency.format(salesTax) + "\n" + "Invoice total:    "
            + currency.format(total) + "\n";

    System.out.println(message);//  ww w  . j  a v a 2s.  com

}

From source file:ObjectStreams.java

public static void main(String[] args) throws IOException, ClassNotFoundException {

    ObjectOutputStream out = null;
    try {//from  w w  w  . jav  a2  s  . com
        out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(dataFile)));

        out.writeObject(Calendar.getInstance());
        for (int i = 0; i < prices.length; i++) {
            out.writeObject(prices[i]);
            out.writeInt(units[i]);
            out.writeUTF(descs[i]);
        }
    } finally {
        out.close();
    }

    ObjectInputStream in = null;
    try {
        in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(dataFile)));

        Calendar date = null;
        BigDecimal price;
        int unit;
        String desc;
        BigDecimal total = new BigDecimal(0);

        date = (Calendar) in.readObject();

        System.out.format("On %tA, %<tB %<te, %<tY:%n", date);

        try {
            while (true) {
                price = (BigDecimal) in.readObject();
                unit = in.readInt();
                desc = in.readUTF();
                System.out.format("You ordered %d units of %s at $%.2f%n", unit, desc, price);
                total = total.add(price.multiply(new BigDecimal(unit)));
            }
        } catch (EOFException e) {
        }
        System.out.format("For a TOTAL of: $%.2f%n", total);
    } finally {
        in.close();
    }
}

From source file:Main.java

public static Long decimal2Long(BigDecimal money) {
    return (money.multiply(BigDecimal.valueOf(100))).longValue();
}

From source file:Main.java

public static BigDecimal addVatAmount(BigDecimal percentage, BigDecimal amount) {
    return amount.multiply(percentage.divide(HUNDRED));
}

From source file:Main.java

public static BigDecimal multiply(BigDecimal num1, BigDecimal num2) {
    return num1.multiply(num2);
}