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:org.apache.carbondata.core.scan.executor.util.RestructureUtil.java

/**
 * Gets the default value based on the column data type.
 *
 * @param columnSchema/*from w  ww  .  j a v a 2s .co m*/
 * @param defaultValue
 * @return
 */
public static Object getMeasureDefaultValueByType(ColumnSchema columnSchema, byte[] defaultValue) {
    Object measureDefaultValue = null;
    if (!isDefaultValueNull(defaultValue)) {
        String value = null;
        switch (columnSchema.getDataType()) {
        case SHORT:
            value = new String(defaultValue, Charset.forName(CarbonCommonConstants.DEFAULT_CHARSET));
            measureDefaultValue = Short.parseShort(value);
            break;
        case INT:
            value = new String(defaultValue, Charset.forName(CarbonCommonConstants.DEFAULT_CHARSET));
            measureDefaultValue = Integer.parseInt(value);
            break;
        case LONG:
            value = new String(defaultValue, Charset.forName(CarbonCommonConstants.DEFAULT_CHARSET));
            measureDefaultValue = Long.parseLong(value);
            break;
        case DECIMAL:
            BigDecimal decimal = DataTypeUtil.byteToBigDecimal(defaultValue);
            if (columnSchema.getScale() > decimal.scale()) {
                decimal = decimal.setScale(columnSchema.getScale(), RoundingMode.HALF_UP);
            }
            measureDefaultValue = Decimal.apply(decimal);
            break;
        default:
            value = new String(defaultValue, Charset.forName(CarbonCommonConstants.DEFAULT_CHARSET));
            Double parsedValue = Double.valueOf(value);
            if (!Double.isInfinite(parsedValue) && !Double.isNaN(parsedValue)) {
                measureDefaultValue = parsedValue;
            }
        }
    }
    return measureDefaultValue;
}

From source file:com.SCI.centraltoko.utility.UtilityTools.java

public static String formatNumber(BigDecimal value) {
    if (value == null || value.equals(BigDecimal.ZERO)) {
        return "0";
    } else {/*from w w w . java  2 s  .  c o  m*/
        NumberFormat formater = NumberFormat.getInstance();
        formater.setMaximumFractionDigits(0);
        formater.setMinimumFractionDigits(0);
        return formater.format(value.setScale(0, RoundingMode.HALF_EVEN));
    }
}

From source file:com.bjond.utilities.MiscUtils.java

/**
 * Function below courtesy of: /*from   ww w  .j a v  a  2  s. co  m*/
 *    * http://stackoverflow.com/questions/2808535/round-a-double-to-2-decimal-places 
* 
* 
* @param value value to round.
* @param places places
* @return the rounded double. 
*/
public static double round(final double value, final 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:com.sfs.Formatter.java

/**
 * Round./*from  w  w w  .ja v  a2s . c o  m*/
 *
 * @param value the value
 * @param decimalPlaces the decimal places
 *
 * @return the double
 */
public static double round(final double value, final int decimalPlaces) {
    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP);
    return bd.doubleValue();
}

From source file:com.sfs.Formatter.java

/**
 * Gets the part number of months based on the number of weeks supplied.
 *
 * @param weeks the weeks value/*  ww w.ja v  a2  s. com*/
 * @return the part months
 */
public static int getPartMonths(final int weeks) {

    BigDecimal bd = new BigDecimal(weeks / WEEKS_IN_MONTH);
    bd = bd.setScale(0, BigDecimal.ROUND_DOWN);

    return (int) bd.intValue();
}

From source file:com.sunchenbin.store.feilong.core.lang.NumberUtil.java

/**
 * ./* ww w.j  a  v a2  s  .  c o  m*/
 * 
 * @param number
 *            number
 * @param scale
 *            ??
 * @param roundingMode
 *            ? {@link RoundingMode} ?: {@link <a href="#RoundingMode">JAVA 8??</a>}
 * @return the big decimal
 * @see <a href="#RoundingMode">JAVA 8??</a>
 */
private static BigDecimal setScale(BigDecimal number, int scale, RoundingMode roundingMode) {
    return number.setScale(scale, roundingMode);
}

From source file:com.feilong.core.lang.NumberUtil.java

/**
 * .//w  w w .  j  a  va  2s. c o m
 * 
 * @param value
 *            number
 * @param scale
 *            ,??,see {@link java.math.BigDecimal#setScale(int, RoundingMode)}
 * @param roundingMode
 *            ? {@link RoundingMode} ?:<a href="#RoundingMode">JAVA 8??</a>
 * @return  <code>value</code> null, {@link NullPointerException}<br>
 *          <code>roundingMode</code>null, {@link NullPointerException}
 * @see <a href="#RoundingMode">JAVA 8??</a>
 */
private static BigDecimal setScale(BigDecimal value, int scale, RoundingMode roundingMode) {
    Validate.notNull(value, "value can't be null!");
    Validate.notNull(roundingMode, "roundingMode can't be null!");
    return value.setScale(scale, roundingMode);
}

From source file:com.vmware.bdd.cli.commands.CommandsUtils.java

/**
 * Take the accuracy of double data./*from   ww  w.j  a  va2s.  c  om*/
 * <p>
 * For example: <br>
 * A double value = 100.345678; <br>
 * The Double ret = round (value, 4, BigDecimal.ROUND_HALF_UP); <br>
 * "Ret 100.3457 <br>
 *
 * @param value
 *           Double data value. @param scale Precision digits (reserve of
 *           decimal digits).
 * @param roundingMode
 *           Precision value way.
 * @return Precision calculation of data.
 */
private static double round(double value, int scale, int roundingMode) {
    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(scale, roundingMode);
    double d = bd.doubleValue();
    return d;
}

From source file:com.almunt.jgcaap.systemupdater.MainActivity.java

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

/**
 * Compute e^x to a given scale. Break x into its whole and fraction parts
 * and compute (e^(1 + fraction/whole))^whole using Taylor's formula.
 * /*from   w w  w .jav  a2s.  c o  m*/
 * @param x
 *            the value of x
 * @param scale
 *            the desired scale of the result
 * @return the result value
 */
public static BigDecimal exp(BigDecimal x, int scale) {
    // e^0 = 1
    if (x.signum() == 0) {
        return BigDecimal.valueOf(1);
    }

    // If x is negative, return 1/(e^-x).
    else if (x.signum() == -1) {
        return BigDecimal.valueOf(1).divide(exp(x.negate(), scale), scale, BigDecimal.ROUND_HALF_EVEN);
    }

    // Compute the whole part of x.
    BigDecimal xWhole = x.setScale(0, BigDecimal.ROUND_DOWN);

    // If there isn't a whole part, compute and return e^x.
    if (xWhole.signum() == 0) {
        return expTaylor(x, scale);
    }

    // Compute the fraction part of x.
    BigDecimal xFraction = x.subtract(xWhole);

    // z = 1 + fraction/whole
    BigDecimal z = BigDecimal.valueOf(1).add(xFraction.divide(xWhole, scale, BigDecimal.ROUND_HALF_EVEN));

    // t = e^z
    BigDecimal t = expTaylor(z, scale);

    BigDecimal maxLong = BigDecimal.valueOf(Long.MAX_VALUE);
    BigDecimal result = BigDecimal.valueOf(1);

    // Compute and return t^whole using intPower().
    // If whole > Long.MAX_VALUE, then first compute products
    // of e^Long.MAX_VALUE.
    while (xWhole.compareTo(maxLong) >= 0) {
        result = result.multiply(intPower(t, Long.MAX_VALUE, scale)).setScale(scale,
                BigDecimal.ROUND_HALF_EVEN);
        xWhole = xWhole.subtract(maxLong);

        Thread.yield();
    }
    return result.multiply(intPower(t, xWhole.longValue(), scale)).setScale(scale, BigDecimal.ROUND_HALF_EVEN);
}