Example usage for java.math BigDecimal subtract

List of usage examples for java.math BigDecimal subtract

Introduction

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

Prototype

public BigDecimal subtract(BigDecimal subtrahend) 

Source Link

Document

Returns a BigDecimal whose value is (this - subtrahend) , and whose scale is max(this.scale(), subtrahend.scale()) .

Usage

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.subtract(bd2);

}

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("100.123");
    BigDecimal bg2 = new BigDecimal("50.56");

    // subtract bg1 with bg2 and assign result to bg3
    BigDecimal bg3 = bg1.subtract(bg2);

    String str = "The Result of Subtraction is " + bg3;

    // print bg3 value
    System.out.println(str);/*  ww w .  j  a v  a  2s .c  om*/
}

From source file:MainClass.java

public static void main(String argv[]) {
    BigDecimal first = new BigDecimal("3419229223372036854775807.23343");
    BigDecimal second = new BigDecimal("2.0");
    System.out.println(first.add(second));
    System.out.println(first.subtract(second));
    System.out.println(first.divide(second));
    System.out.println(first.equals(second));
    System.out.println(first.abs());
    System.out.println(first.max(second));
    System.out.println(first.min(second));
    System.out.println(first.remainder(second));
}

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   w ww.  ja  v a 2s .  c o m*/
    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  2s . 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);/*from w  w w  .jav  a2  s  . c  o  m*/

}

From source file:Main.java

public static BigDecimal subtract(BigDecimal num1, BigDecimal num2) {
    // TODO Auto-generated method stub
    return num1.subtract(num2);
}

From source file:Main.java

public static List<BigDecimal> intervals(final List<BigDecimal> list) {
    // list must be sorted for interval calculation
    Collections.sort(list);//  www.  j a  va 2s  .  co m
    final List<BigDecimal> retVal = new ArrayList<BigDecimal>();
    BigDecimal before = list.get(0);
    for (int i = 1; i < list.size(); i++) {
        final BigDecimal current = list.get(i);
        retVal.add(current.subtract(before));
        before = current;
    }
    return retVal;
}

From source file:Main.java

public static double sub(String d1, String d2) {
    BigDecimal b1 = new BigDecimal(d1);
    BigDecimal b2 = new BigDecimal(d2);
    return b1.subtract(b2).doubleValue();

}

From source file:Main.java

public static final double sub(String v1, String v2) {
    BigDecimal b1 = new BigDecimal(v1);
    BigDecimal b2 = new BigDecimal(v2);
    return (b1.subtract(b2)).doubleValue();
}