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

private static BigDecimal round(double value, int decimals) {
    BigDecimal valueBigDecimal = new BigDecimal(value);
    return valueBigDecimal.setScale(decimals, BigDecimal.ROUND_HALF_UP);
}

From source file:Main.java

public static float roundFloat(float number, int count) {
    BigDecimal bd = new BigDecimal(number + "");
    float value = bd.setScale(count, BigDecimal.ROUND_HALF_UP).floatValue();
    return value;
}

From source file:Main.java

public static String keepZeroSecimal(String numberStr) {
    double number = Double.parseDouble(numberStr);
    BigDecimal b = new BigDecimal(number);

    String ss = b.setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue() + "";

    return ss;//  w  ww. j  a  va  2  s .co m
}

From source file:Main.java

/**
 * Rounds a double down via passed in amount and places
 * @param value/*from www.  j a v a 2s .co  m*/
 * @param places
 * @return
 */
public static double roundDown(double value, int places) {
    if (places < 0)
        throw new IllegalArgumentException();

    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(places, RoundingMode.FLOOR);
    return bd.doubleValue();
}

From source file:Main.java

/**
 * Rounds a double via passed in amount and places
 * @param value/*w  ww.  j av a2  s.  c  o  m*/
 * @param places
 * @return
 */
public static double round(double value, int places) {
    if (places < 0)
        throw new IllegalArgumentException();

    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(places, RoundingMode.HALF_UP);
    return bd.doubleValue();
}

From source file:Main.java

/**
 * Rounds a double up via passed in amount and places
 * @param value/* w w  w. j  a v  a 2s.  c o  m*/
 * @param places
 * @return
 */
public static double roundUp(double value, int places) {
    if (places < 0)
        throw new IllegalArgumentException();

    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(places, RoundingMode.CEILING);
    return bd.doubleValue();
}

From source file:Main.java

private static Float precision(int decimalPlace, Float d) {

    BigDecimal bd = new BigDecimal(Float.toString(d));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
    return bd.floatValue();
}

From source file:Main.java

public static double round(double num) {
    BigDecimal b = new BigDecimal(num);
    BigDecimal rounded = b.setScale(2, RoundingMode.HALF_EVEN);
    return rounded.doubleValue();

    //return num;
}

From source file:Main.java

public static Object round(Object value, int places) {
    // if (places < 0) throw new IllegalArgumentException();
    if (places < 0 || !(value instanceof Double)) {
        return value;
    }//from w  w w .  ja  v  a 2  s  .c o  m
    BigDecimal bd = new BigDecimal((Double) value);
    bd = bd.setScale(places, BigDecimal.ROUND_HALF_UP);
    return bd.doubleValue();
}

From source file:Main.java

public static double round(double d, int decimalPlace) {
    // see the Javadoc about why we use a String in the constructor
    // http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html#BigDecimal(double)
    BigDecimal bd = new BigDecimal(Double.toString(d));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
    return bd.doubleValue();
}