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:org.kuali.coeus.s2sgen.impl.datetime.S2SDateTimeServiceImpl.java

/**
 *
 * This method computes the number of months between any 2 given {@link java.sql.Date} objects
 *
 * @param dateStart starting date.//from w  w  w  .ja  va2 s . c o  m
 * @param dateEnd end date.
 *
 * @return number of months between the start date and end date.
 */
@Override
public ScaleTwoDecimal getNumberOfMonths(java.util.Date dateStart, java.util.Date dateEnd) {
    ScaleTwoDecimal monthCount = ScaleTwoDecimal.ZERO;
    int fullMonthCount = 0;

    Calendar startDate = Calendar.getInstance();
    Calendar endDate = Calendar.getInstance();
    startDate.setTime(dateStart);
    endDate.setTime(dateEnd);

    startDate.clear(Calendar.HOUR);
    startDate.clear(Calendar.MINUTE);
    startDate.clear(Calendar.SECOND);
    startDate.clear(Calendar.MILLISECOND);

    endDate.clear(Calendar.HOUR);
    endDate.clear(Calendar.MINUTE);
    endDate.clear(Calendar.SECOND);
    endDate.clear(Calendar.MILLISECOND);

    if (startDate.after(endDate)) {
        return ScaleTwoDecimal.ZERO;
    }
    int startMonthDays = startDate.getActualMaximum(Calendar.DATE) - startDate.get(Calendar.DATE);
    startMonthDays++;
    int startMonthMaxDays = startDate.getActualMaximum(Calendar.DATE);
    BigDecimal startMonthFraction = BigDecimal.valueOf(startMonthDays).setScale(2, RoundingMode.HALF_UP).divide(
            BigDecimal.valueOf(startMonthMaxDays).setScale(2, RoundingMode.HALF_UP), RoundingMode.HALF_UP);

    int endMonthDays = endDate.get(Calendar.DATE);
    int endMonthMaxDays = endDate.getActualMaximum(Calendar.DATE);

    BigDecimal endMonthFraction = BigDecimal.valueOf(endMonthDays).setScale(2, RoundingMode.HALF_UP).divide(
            BigDecimal.valueOf(endMonthMaxDays).setScale(2, RoundingMode.HALF_UP), RoundingMode.HALF_UP);

    startDate.set(Calendar.DATE, 1);
    endDate.set(Calendar.DATE, 1);

    while (startDate.getTimeInMillis() < endDate.getTimeInMillis()) {
        startDate.set(Calendar.MONTH, startDate.get(Calendar.MONTH) + 1);
        fullMonthCount++;
    }
    fullMonthCount = fullMonthCount - 1;
    monthCount = monthCount.add(new ScaleTwoDecimal(fullMonthCount))
            .add(new ScaleTwoDecimal(startMonthFraction)).add(new ScaleTwoDecimal(endMonthFraction));
    return monthCount;
}

From source file:bg.elkabel.calculator.service.CoreServiceImpl.java

private double calculateWeight(Material material, double coreSize) {
    double result = 0.0;
    switch (material.ordinal()) {
    case 0://w ww  .  ja v  a  2 s  . co  m
        result = Math.pow(coreSize, 2) * Math.PI / 4 * 2.7 * 1 / 1000;
        break;
    case 1:
        result = Math.pow(coreSize, 2) * Math.PI / 4 * 8.9 * 1 / 1000;
        break;
    }

    return BigDecimal.valueOf(result).setScale(6, RoundingMode.HALF_UP).doubleValue();
}

From source file:com.enadein.carlogbook.db.CommonUtils.java

public static void setupDecimalFormat() {
    formatP = createDecimalFormat(1, 3, 0, 2, '.', RoundingMode.HALF_UP);
    formatPComma = createDecimalFormat(1, 3, 0, 2, ',', RoundingMode.HALF_UP);
    formatDistance = createDecimalFormat(1, 16, 0, 0, '.');

    format = createDecimalFormat(1, 16, UnitFacade.currencyFract, UnitFacade.currencyFract, '.');
    formatComma = createDecimalFormat(1, 16, UnitFacade.currencyFract, UnitFacade.currencyFract, ',');

    formatFuel = createDecimalFormat(1, 16, UnitFacade.fuelFract, UnitFacade.fuelFract, '.');
    formatFuelComma = createDecimalFormat(1, 16, UnitFacade.fuelFract, UnitFacade.fuelFract, ',');
}

From source file:services.kpi.PortfolioEntryActualsKpi.java

@Override
public BigDecimal computeAdditional2(IPreferenceManagerPlugin preferenceManagerPlugin,
        IScriptService scriptService, Kpi kpi, Long objectId) {
    Double entryEngagedOpex = PortfolioEntryDao.getPEAsEngagedAmountByOpex(preferenceManagerPlugin, objectId,
            true);//w  w  w.j  a va2  s. co m
    BigDecimal engagedOpex = (new BigDecimal(entryEngagedOpex)).setScale(2, RoundingMode.HALF_UP);
    return engagedOpex;
}

From source file:com.navercorp.pinpoint.web.mapper.stat.sampling.sampler.JoinCpuLoadSampler.java

private double roundToScale(double value) {
    return BigDecimal.valueOf(value).setScale(1, RoundingMode.HALF_UP).doubleValue();
}

From source file:org.zkoss.ganttz.DatesMapperOnInterval.java

private int toPixels(Fraction proportion) {
    try {/*from   ww w.  ja  va  2 s  . com*/
        return proportion.multiplyBy(Fraction.getFraction(horizontalSize, 1)).intValue();
    } catch (ArithmeticException e) {
        double d = Math.log10(horizontalSize);
        int scale = (int) d + 1;

        BigDecimal quotient = new BigDecimal(proportion.getNumerator())
                .divide(new BigDecimal(proportion.getDenominator()), scale, RoundingMode.HALF_UP);

        return quotient.multiply(new BigDecimal(horizontalSize)).intValue();
    }
}

From source file:org.apache.carbondata.core.util.DataTypeUtil.java

/**
 * This method will convert a given value to its specific type
 *
 * @param msrValue/*from  www.j a  v  a  2  s .  c o m*/
 * @param dataType
 * @param carbonMeasure
 * @return
 */
public static Object getMeasureValueBasedOnDataType(String msrValue, DataType dataType,
        CarbonMeasure carbonMeasure) {
    switch (dataType) {
    case DECIMAL:
        BigDecimal bigDecimal = new BigDecimal(msrValue).setScale(carbonMeasure.getScale(),
                RoundingMode.HALF_UP);
        return normalizeDecimalValue(bigDecimal, carbonMeasure.getPrecision());
    case INT:
        return Double.valueOf(msrValue).longValue();
    case LONG:
        return Long.valueOf(msrValue);
    default:
        Double parsedValue = Double.valueOf(msrValue);
        if (Double.isInfinite(parsedValue) || Double.isNaN(parsedValue)) {
            return null;
        }
        return parsedValue;
    }
}

From source file:nl.strohalm.cyclos.utils.conversion.FixedLengthNumberConverter.java

public T valueOf(final String string) {
    if (StringUtils.isEmpty(string)) {
        return null;
    }//from   w  w  w. j a  v a 2 s.  c  om
    if (!StringUtils.isNumeric(string)) {
        throw new ConversionException("Invalid string. Expecting only numbers, but received " + string);
    }
    final BigDecimal number = new BigDecimal(new BigInteger(string)).divide(divisor, scale,
            RoundingMode.HALF_UP);
    return CoercionHelper.coerce(numberType, number);
}

From source file:org.projectforge.web.wicket.converter.CurrencyConverter.java

/**
 * If total amount is given also a percentage value is supported.
 * @see org.apache.wicket.util.convert.converters.BigDecimalConverter#convertToObject(java.lang.String, java.util.Locale)
 *//*  w  w  w  . j av  a  2s.c o  m*/
@Override
public BigDecimal convertToObject(String value, final Locale locale) {
    value = StringUtils.trimToEmpty(value);
    if (value.endsWith(currency) == true) {
        value = value.substring(0, value.length() - 1).trim();
    } else if (totalAmount != null && value.endsWith("%") == true) {
        value = value.substring(0, value.length() - 1).trim();
        final BigDecimal percentage = super.convertToObject(value, locale);
        return totalAmount.multiply(percentage).divide(NumberHelper.HUNDRED, RoundingMode.HALF_UP);
    }
    return super.convertToObject(value, locale);
}

From source file:com.qcadoo.mes.workPlans.pdf.document.operation.product.column.QuantityPerUnitOperationProductColumn.java

@Override
public String getColumnValue(Entity operationProduct) {
    List<Entity> orders = operationProduct
            .getBelongsToField(OperationProductInComponentFields.OPERATION_COMPONENT)
            .getBelongsToField(TechnologyOperationComponentFields.TECHNOLOGY)
            .getHasManyField(TechnologyFieldsO.ORDERS);
    if (orders.isEmpty()) {
        return StringUtils.EMPTY;
    }//from   w  w w  . ja va  2 s . c  o  m
    return String.valueOf(numberService.format(numberService
            .setScale((operationProduct.getDecimalField(OperationProductInComponentFields.QUANTITY).divide(
                    orders.get(0).getDecimalField(OrderFields.PLANNED_QUANTITY), RoundingMode.HALF_UP)))));
}