Example usage for java.math BigDecimal setScale

List of usage examples for java.math BigDecimal setScale

Introduction

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

Prototype

@Deprecated(since = "9")
public BigDecimal setScale(int newScale, int roundingMode) 

Source Link

Document

Returns a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal 's unscaled value by the appropriate power of ten to maintain its overall value.

Usage

From source file:Main.java

public static void main(String args[]) {
    double r = 3.1234567;
    int decimalPlace = 2;
    BigDecimal bd = new BigDecimal(r);
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_UP);
    r = bd.doubleValue();//www .  j a  va  2 s  . c  om
    System.out.println(r);
}

From source file:Main.java

public static void main(String args[]) {
    BigDecimal bd = new BigDecimal(3.14159);
    bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);

    System.out.println(bd);/*w  ww . j  a v a2  s .  c om*/
}

From source file:Main.java

public static void main(String[] args) {
    double operation = 890.0 / 1440.0;
    BigDecimal big = new BigDecimal(operation);
    big = big.setScale(4, RoundingMode.HALF_UP);
    double d2 = big.doubleValue();
    System.out.println(String.format("operation : %s", operation));
    System.out.println(String.format("scaled : %s", d2));
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int decimalPlaces = 2;
    BigDecimal bd = new BigDecimal("123456789.0123456890");

    bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_DOWN);
    String string = bd.toString();
}

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);//  w ww.ja v  a2  s.  co m

}

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("123.12678");

    // set scale of bg1 to 2 in bg2 using floor as rounding mode
    BigDecimal bg2 = bg1.setScale(2, RoundingMode.FLOOR);

    String str = bg1 + " after changing the scale to 2 and rounding is " + bg2;

    // print bg2 value
    System.out.println(str);// ww w . jav  a  2s . c  om
}

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("123.12345678");

    // set scale of bg1 to 2 in bg2
    // 0 specifies ROUND_UP
    BigDecimal bg2 = bg1.setScale(2, RoundingMode.UP);

    String str = bg1 + " after changing the scale to 2 and rounding is " + bg2;

    System.out.println(str);//from   www .  j  a v  a2  s  .  c  o  m
}

From source file:Main.java

public static double add(double v1, double v2) {
    BigDecimal a = new BigDecimal(v1);
    BigDecimal b = new BigDecimal(v2);
    BigDecimal c = a.add(b);
    return c.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
}

From source file:Main.java

public static double sub(double v1, double v2) {
    BigDecimal a = new BigDecimal(v1);
    BigDecimal b = new BigDecimal(v2);
    BigDecimal c = a.subtract(b);
    c.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    return c.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
}

From source file:Main.java

public static double Rounding(double d) {
    BigDecimal bd = new BigDecimal(d);
    bd.setScale(1, RoundingMode.HALF_UP);
    return bd.floatValue();
}