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:de.andreasschoknecht.LS3.LSSMCalculator.java

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:org.egov.works.services.WorkProgressAbstractReportService.java

/**
 * Converting given amount to show in Crores with no of decimal points to be rounded off
 *
 * @param amount//from ww w. ja va 2s  .  c  om
 * @param decimalPoints
 * @return
 */
public BigDecimal getRoundedOfAmount(final Object amount, final int decimalPoints) {
    final int dividingFactor = 10000000; // 1 Crore
    if (amount != null) {
        final BigDecimal divisor = new BigDecimal(dividingFactor);
        final BigDecimal amountBD = (BigDecimal) amount;
        final BigDecimal result = amountBD.divide(divisor);
        return result.setScale(decimalPoints, RoundingMode.HALF_UP);
    } else
        return null;
}

From source file:org.libreplan.business.workingday.ResourcesPerDay.java

private ResourcesPerDay(BigDecimal amount) {
    Validate.isTrue(amount.intValue() >= 0);
    this.amount = amount.setScale(4, RoundingMode.HALF_UP);
}

From source file:de.kopis.glacier.util.VaultInventoryPrinter.java

private String round(final String unrounded, final int precision, final int roundingMode) {
    final BigDecimal bd = new BigDecimal(unrounded);
    final BigDecimal rounded = bd.setScale(precision, roundingMode);
    return rounded.toString();
}

From source file:com.sequenceiq.ambari.shell.flash.InstallProgress.java

@Override
public String getText() {
    StringBuilder sb = new StringBuilder();
    if (!done) {/*w w w . ja  v a  2  s.  co  m*/
        BigDecimal progress = client.getRequestProgress();
        if (progress != null) {
            BigDecimal decimal = progress.setScale(2, BigDecimal.ROUND_HALF_UP);
            int intValue = decimal.intValue();
            if (intValue != SUCCESS && intValue != FAILED) {
                sb.append("Installation: ").append(decimal).append("% ");
                int rounded = round(progress.setScale(0, BigDecimal.ROUND_UP).intValue() / 10);
                for (int i = 0; i < 10; i++) {
                    if (i < rounded) {
                        sb.append("=");
                    } else {
                        sb.append("-");
                    }
                }
            } else if (intValue == FAILED) {
                sb.append("Installation: FAILED");
                done = true;
            } else {
                sb.append("Installation: COMPLETE");
                done = true;
            }
        } else {
            sb.append("Installation: WAITING..");
        }
    } else {
        if (exit) {
            System.exit(0);
        }
    }
    return sb.toString();
}

From source file:com.panet.imeta.core.row.ValueDataUtil.java

public static Object round(ValueMetaInterface metaA, Object dataA, ValueMetaInterface metaB, Object dataB)
        throws KettleValueException {
    if (dataA == null || dataB == null)
        return null;

    switch (metaA.getType()) {
    case ValueMetaInterface.TYPE_NUMBER:
        return new Double(
                Const.round(metaA.getNumber(dataA).doubleValue(), metaB.getInteger(dataB).intValue()));
    case ValueMetaInterface.TYPE_INTEGER:
        return metaA.getInteger(dataA);
    case ValueMetaInterface.TYPE_BIGNUMBER:

        // Round it to the desired number of digits.
        BigDecimal number = metaA.getBigNumber(dataA);
        return number.setScale(metaB.getInteger(dataB).intValue(), BigDecimal.ROUND_HALF_EVEN);

    default:/*from   ww w.ja va2 s  . c  o  m*/
        throw new KettleValueException("The 'round' function only works on numeric data");
    }
}

From source file:org.kalypso.gml.ui.internal.coverage.CoverageColorRangeAction.java

/**
 * protect against too many digits; still ugly, as number of digits depends on real data type...
 *//*from w ww . ja va  2  s. c om*/
private BigDecimal asBigDecimal(final Double value) {
    final BigDecimal decimal = new BigDecimal(Double.toString(value));
    if (decimal.scale() < 5)
        return decimal;

    return decimal.setScale(4, BigDecimal.ROUND_HALF_UP);
}

From source file:com.zhihu.android.app.mirror.widget.ArtboardView.java

public boolean isScaling() {
    float scale;//from   w w  w  .  j a v  a  2  s.  co m
    float minScale;
    try {
        BigDecimal decimal = new BigDecimal(getScale());
        scale = decimal.setScale(2, BigDecimal.ROUND_FLOOR).floatValue();
        decimal = new BigDecimal(getMinScale());
        minScale = decimal.setScale(2, BigDecimal.ROUND_FLOOR).floatValue();
        return scale > minScale;
    } catch (NumberFormatException e) {
        e.printStackTrace();
        return false;
    }
}

From source file:com.vangent.hieos.empi.match.ScoredRecord.java

/**
 * //from w  ww .j  a  va 2s . com
 * @return
 */
public int getMatchScorePercentage() {
    // Round-up match score.
    BigDecimal bd = new BigDecimal(this.getScore() * 100.0);
    bd = bd.setScale(0, BigDecimal.ROUND_HALF_UP);
    return bd.intValue();
}

From source file:org.apache.hadoop.hive.ql.udf.UDFTRUNC.java

public DoubleWritable evaluate(DoubleWritable val) {
    if (val == null)
        return null;
    try {//  w  w  w. ja va 2s . c om
        DoubleWritable re = new DoubleWritable();
        double d = val.get();
        BigDecimal big = new BigDecimal(d);
        big = big.setScale(0, 1);
        re.set(big.doubleValue());
        return re;
    } catch (Exception e) {
        return null;
    }
}