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 Float getFloatFromFloatRoundHalfUp(float sourceNum, int scale) {
    BigDecimal bigDecimal = new BigDecimal(sourceNum);
    return bigDecimal.setScale(scale, BigDecimal.ROUND_HALF_UP).floatValue();
}

From source file:Main.java

public static String str2Bi(String d) {
    BigDecimal bigDecimal = new BigDecimal(d);
    bigDecimal = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP);
    return bigDecimal.toString();
}

From source file:Main.java

public static BigDecimal round(float d, int decimalPlace) {
    BigDecimal bd = new BigDecimal(Float.toString(d));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
    return bd;/* w  w  w .ja va 2s. c  o m*/
}

From source file:Main.java

public static float round(float d, int decimalPlace) {
    BigDecimal bd = new BigDecimal(Float.toString(d));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_FLOOR);
    return bd.floatValue();
}

From source file:Main.java

public static float handleFloat(float num, int limit) {
    BigDecimal bigDecimal = new BigDecimal(num);
    float res = bigDecimal.setScale(limit, BigDecimal.ROUND_HALF_UP).floatValue();
    return res;//from   w ww  .  j a v a2s.co  m
}

From source file:Main.java

/**
 * Round double value with low error//w w  w .ja  va 2 s  .co m
 *
 * @param value  Value
 * @param places decimal
 * @return double
 */
static double round(double value, int places) {
    // check if places below 0
    places = Math.max(0, places);
    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(places, RoundingMode.HALF_UP);
    return bd.doubleValue();
}

From source file:Main.java

public static int getNumericByDouble(double value) {
    BigDecimal valueBigDecimal = new BigDecimal(value);
    return valueBigDecimal.setScale(0, BigDecimal.ROUND_DOWN).intValue();
}

From source file:Main.java

public static String convertOneDecimal(double price) {
    BigDecimal decimal = new BigDecimal(price);
    return decimal.setScale(1, BigDecimal.ROUND_HALF_UP).toString();
}

From source file:Main.java

public static String convertTwoDecimal(double price) {
    BigDecimal decimal = new BigDecimal(price);
    return decimal.setScale(2, BigDecimal.ROUND_HALF_UP).toString();
}

From source file:Main.java

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