Example usage for java.math RoundingMode HALF_UP

List of usage examples for java.math RoundingMode HALF_UP

Introduction

In this page you can find the example usage for java.math RoundingMode HALF_UP.

Prototype

RoundingMode HALF_UP

To view the source code for java.math RoundingMode HALF_UP.

Click Source Link

Document

Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.

Usage

From source file:au.org.ala.delta.model.attribute.SignificantFiguresAttributeChunkFormatter.java

/**
 * Overrides formatNumber in the parent class to format the number to 5 significant figures.  Trailing
 * zeros are stripped./*from  w  ww  .  j a v  a 2  s  .  c om*/
 * Note: for compatibility with the original CONFOR significant figures are only applied to values after
 * the decimal place. (e.g. 123456.7 will be formatted as 123456, not 123460)
 * @param number the number to format.
 * @return the supplied number as a String.
 */
@Override
public String formatNumber(BigDecimal number) {

    int significantFigures = determinePrecision(number);
    MathContext context = new MathContext(significantFigures, RoundingMode.HALF_UP);

    BigDecimal result = number.round(context);
    result = result.stripTrailingZeros();
    return result.toPlainString();
}

From source file:fakingXmocking.CurrencyConversionHttpClientFake.java

private String formatResultContainingCurrencyConversion(String[] params) {
    String from = getParameter(params, "from");
    String to = getParameter(params, "to");
    BigDecimal rate = findConversionRate(from, to);

    currenciesAndRates.put(from + '>' + to, rate.setScale(2));
    currenciesAndRates.put(to + '>' + from, BigDecimal.ONE.divide(rate, 2, RoundingMode.HALF_UP));

    return "<div id=\"converter_results\"><ul><li><b>1 " + from + " = " + rate + ' ' + to + "</b>";
}

From source file:com.feilong.core.text.NumberFormatUtil.java

/**
 *  {@link Number}  numberPattern?.//from  ww  w .j  ava  2  s  .c o  m
 * 
 * <p style="color:red">
 * ?, {@link com.feilong.core.lang.NumberUtil#toString(Number, String) NumberUtil.toString(Number, String)}
 * </p>
 * 
 * <p>
 *  {@link java.math.RoundingMode#HALF_UP}
 * </p>
 *
 * @param value
 *            the value
 * @param numberPattern
 *            the pattern {@link NumberPattern}
 * @return  <code>value</code> null, {@link NullPointerException}<br>
 *          <code>numberPattern</code> null, {@link NullPointerException}<br>
 *          <code>numberPattern</code> blank, {@link IllegalArgumentException}<br>
 * @see NumberPattern
 * @see DecimalFormat
 * @see RoundingMode#HALF_UP
 * @see #format(Number, String, RoundingMode)
 */
public static String format(Number value, String numberPattern) {
    RoundingMode roundingMode = RoundingMode.HALF_UP;
    return format(value, numberPattern, roundingMode);
}

From source file:ec.edu.distri.clientejava.protocolo.model.Canal.java

@Override
public boolean build(String input) {
    if (validate(input) == true) {
        try {//from   w  ww  .j a va  2  s .  c om
            String values[] = MyStringUtil.splitByFixedLengths(input, new int[] { 25, 100, 5 });
            this.nombre = values[0];
            this.descripcion = values[1];
            costo = new BigDecimal(values[2]).setScale(2, RoundingMode.HALF_UP);

            return true;
        } catch (Exception e) {

            // e.printStackTrace();
            System.out.println("" + e);
        }
    }
    return false;
}

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

public static ResourcesPerDay calculateFrom(EffortDuration durationWorking, EffortDuration durationWorkable) {
    return amount(new BigDecimal(durationWorking.getSeconds())
            .divide(new BigDecimal(durationWorkable.getSeconds()), 4, RoundingMode.HALF_UP));
}

From source file:org.wso2.carbon.pc.analytics.core.generic.utils.AnalyticsUtils.java

/**
 * Round a double value to two decimal points
 *
 * @param value  double value/*w w w .j  av  a 2 s .co  m*/
 * @param places number of decimals
 * @return rounded decimal value
 */
private 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:org.esupportail.papercut.domain.UserPapercutInfos.java

/**
 * Papercut WebService gives balance values like 1.9465000000000003 
 * Error of epsilon machine is suspected in Papercut !
 * -> to workaround, we simply here round the balance ... 
 * /*from  w  ww  . j  a  v a 2  s.c  om*/
 * Note that 5 decimals is the max in papercut today, but maybe one day it will be more so here we return with 10 max 
 * 
 * @param balance
 * @return
 */
private static String fixRoundPapercutError(String balance) {
    DecimalFormat df = new DecimalFormat("#.##########");
    df.setRoundingMode(RoundingMode.HALF_UP);
    return df.format(Double.valueOf(balance));
}

From source file:org.openhab.binding.homematic.internal.converter.state.AbstractTypeConverter.java

/**
 * Rounds a double value./* w ww.  j  a v a  2  s  .  c  o m*/
 */
protected BigDecimal round(Double number) {
    BigDecimal bd = new BigDecimal(number == null ? "0" : number.toString());
    String stringBd = bd.toPlainString();
    int scale = stringBd.length() - (stringBd.lastIndexOf('.') + 1);
    return bd.setScale(scale > 2 ? 6 : 2, RoundingMode.HALF_UP);
}

From source file:com.ocs.dynamo.ui.converter.BigDecimalConverter.java

@Override
public BigDecimal convertToModel(String value, Class<? extends BigDecimal> targetType, Locale locale) {
    // the original Vaadin code curiously returns a Double here and casts
    // that to a BigDecimal.
    // That is not correct, so we use this additional step here
    Number number = convertToNumber(value, BigDecimal.class, locale);
    return number == null ? null
            : BigDecimal.valueOf(number.doubleValue()).setScale(precision, RoundingMode.HALF_UP);
}

From source file:services.kpi.PortfolioEntryActualsKpi.java

@Override
public BigDecimal computeAdditional1(IPreferenceManagerPlugin preferenceManagerPlugin,
        IScriptService scriptService, Kpi kpi, Long objectId) {
    Double entryEngagedCapex = PortfolioEntryDao.getPEAsEngagedAmountByOpex(preferenceManagerPlugin, objectId,
            false);//from  w  w  w  .  j a  v a  2s .c  om
    BigDecimal engagedCapex = (new BigDecimal(entryEngagedCapex)).setScale(2, RoundingMode.HALF_UP);
    return engagedCapex;
}