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:it.polimi.diceH2020.launcher.controller.LaunchAnalysis.java

/**
 * Round doubles without losing precision.
 *
 * @param unrounded/*w w w . ja  v  a  2 s  . c  o  m*/
 * @param precision
 * @param roundingMode
 * @return
 */
public static double round(double unrounded, int precision, int roundingMode) {
    BigDecimal bd = new BigDecimal(unrounded);
    BigDecimal rounded = bd.setScale(precision, roundingMode);
    return rounded.doubleValue();
}

From source file:com.salesmanager.core.util.CurrencyUtil.java

/**
 * Get the measure according to the appropriate measure base. If the measure
 * configured in store is IN and it needs CM or vise versa then the
 * appropriate calculation is done//from  w w  w .  ja  v  a 2 s. c o m
 * 
 * @param weight
 * @param store
 * @param base
 * @return
 */
public static double getMeasure(double measure, MerchantStore store, String base) {

    if (base.equals(Constants.INCH_SIZE_UNIT)) {
        if (store.getSeizeunitcode().equals(Constants.INCH_SIZE_UNIT)) {
            return new BigDecimal(String.valueOf(measure)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        } else {// centimeter (inch to centimeter)
            double measureConstant = 2.54;

            double answer = measure * measureConstant;
            BigDecimal w = new BigDecimal(answer);
            return w.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

        }
    } else {// need CM
        if (store.getSeizeunitcode().equals(Constants.CM_SIZE_UNIT)) {
            return new BigDecimal(String.valueOf(measure)).setScale(2).doubleValue();
        } else {// in (centimeter to inch)
            double measureConstant = 0.39;

            double answer = measure * measureConstant;
            BigDecimal w = new BigDecimal(answer);
            return w.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

        }
    }

}

From source file:com.salesmanager.core.util.CurrencyUtil.java

public static BigDecimal validateMeasure(String measure, String currencycode) throws ValidationException {

    try {/*from  w  w w  .  j  a va2 s.c  om*/

        if (currencycode == null) {
            currencycode = getDefaultCurrency();
        }

        log.debug("Trying to validate " + measure + " for currency " + currencycode);

        CurrencyModule module = (CurrencyModule) currencyMap.get(currencycode);

        if (module == null) {
            throw new ValidationException("There is no CurrencyModule defined for currency " + currencycode
                    + " in module/impl/application/currencies");
        }

        BigDecimal returnMeasure = module.getAmount(measure);
        returnMeasure.setScale(1, BigDecimal.ROUND_HALF_UP);
        return returnMeasure;

    } catch (Exception e) {
        if (e instanceof ValidationException)
            throw (ValidationException) e;
        throw new ValidationException(e.getMessage());
    }

}

From source file:com.mythesis.userbehaviouranalysis.ProfileAnalysis.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:com.wso2telco.core.dbutils.DbUtils.java

/**
 * Format./*  w w  w.j  a  v  a 2s.co m*/
 *
 * @param doubData
 *            the doub data
 * @param precision
 *            the precision
 * @param scale
 *            the scale
 * @return the string
 * @throws Exception
 *             the exception
 */
public static String format(double doubData, int precision, int scale) throws Exception {
    BigDecimal decData = new BigDecimal(doubData);
    decData = decData.setScale(scale, BigDecimal.ROUND_HALF_EVEN);
    String strData = decData.toString();

    // prepare the final string
    int finalLen = precision + 1;
    String finalStr;
    if (finalLen <= strData.length()) {
        finalStr = strData.substring(0, finalLen);
    } else {
        finalStr = "";
        for (int i = 0; i < finalLen - strData.length(); i++) {
            finalStr = finalStr + " ";
        }
        finalStr = finalStr + strData;
    }

    return (finalStr);
}

From source file:com.nubits.nubot.utils.Utils.java

public static double round(double value, int places) {
    if (places < 0) {
        throw new IllegalArgumentException();
    }//from  w ww .  j ava 2  s . c  o m

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

From source file:com.wso2telco.core.dbutils.DbUtils.java

/**
 * Format./* w  ww  . ja va 2s.co  m*/
 *
 * @param decData
 *            the dec data
 * @param precision
 *            the precision
 * @param scale
 *            the scale
 * @return the string
 * @throws Exception
 *             the exception
 */
public static String format(BigDecimal decData, int precision, int scale) throws Exception {
    decData = decData.setScale(scale, BigDecimal.ROUND_HALF_EVEN);
    String strData = decData.toString();

    // prepare the final string
    int finalLen = precision + 1;
    String finalStr;
    if (finalLen <= strData.length()) {
        finalStr = strData.substring(0, finalLen);
    } else {
        finalStr = "";
        for (int i = 0; i < finalLen - strData.length(); i++) {
            finalStr = finalStr + " ";
        }
        finalStr = finalStr + strData;
    }

    return (finalStr);
}

From source file:com.grarak.kerneladiutor.utils.Utils.java

public static double roundTo2Decimals(double val) {
    BigDecimal bd = new BigDecimal(val);
    bd = bd.setScale(2, RoundingMode.HALF_UP);
    return bd.doubleValue();
}

From source file:org.apache.carbondata.core.scan.executor.util.RestructureUtil.java

/**
 * Method for computing measure default value based on the data type
 *
 * @param columnSchema//from   ww  w. j av a2  s .  c o  m
 * @param defaultValue
 * @return
 */
public static Object getMeasureDefaultValue(ColumnSchema columnSchema, byte[] defaultValue) {
    Object measureDefaultValue = null;
    if (!isDefaultValueNull(defaultValue)) {
        String value = null;
        switch (columnSchema.getDataType()) {
        case SHORT:
        case INT:
        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;
            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:Main.java

/**
 * Compute e^x to a given scale.//from   w w w. j a  v  a  2  s.  com
 * Break x into its whole and fraction parts and
 * compute (e^(1 + fraction/whole))^whole using Taylor's formula.
 * @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);
}