Example usage for java.math BigDecimal ROUND_HALF_UP

List of usage examples for java.math BigDecimal ROUND_HALF_UP

Introduction

In this page you can find the example usage for java.math BigDecimal ROUND_HALF_UP.

Prototype

int ROUND_HALF_UP

To view the source code for java.math BigDecimal ROUND_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:de.forsthaus.webui.InitApplicationCtrl.java

/**
 * Add a new row to the grid.<br>/*from ww  w .  jav  a2 s . com*/
 * 
 * @param rowParent
 * @param tableName
 * @param value
 * @param color
 */
private void addNewRow(Rows rowParent, String tableName, Object value, String color) {

    Row row = new Row();

    Html html_TableName = new Html(tableName);
    html_TableName.setStyle("padding-left: 5px; color: " + color + ";");
    Div divKey = new Div();
    divKey.setAlign("left");
    divKey.appendChild(html_TableName);

    Html html_RecordCount = null;

    if (value instanceof BigDecimal) {
        BigDecimal myDec = (BigDecimal) value;
        myDec = myDec.setScale(2, BigDecimal.ROUND_HALF_UP);

        // Format the value to money
        NumberFormat formatter = new DecimalFormat("#,##0.00");

        String money = formatter.format(myDec);

        html_RecordCount = new Html(money);
    } else if (value instanceof Integer) {
        // Format the value
        NumberFormat formatter = new DecimalFormat("###,###.###");
        String formattedInteger = formatter.format(value);
        html_RecordCount = new Html(String.valueOf(value));
    } else
        html_RecordCount = new Html(String.valueOf(value));

    html_RecordCount.setStyle("padding-right: 5px; color: " + color + ";");
    Div divValue = new Div();
    divValue.setAlign("right");
    divValue.appendChild(html_RecordCount);

    row.appendChild(divKey);
    row.appendChild(divValue);
    row.setParent(rowParent);

}

From source file:org.efaps.esjp.accounting.transaction.evaluation.DocumentInfo_Base.java

/**
 * @return the sum in the rate currency/*from  w  ww. j  ava2 s.  c o  m*/
 */
public BigDecimal getRateCreditSum() {
    BigDecimal ret = BigDecimal.ZERO;
    for (final AccountInfo account : getCreditAccounts()) {
        ret = ret.add(account.getAmount().setScale(2, BigDecimal.ROUND_HALF_UP));
    }
    return ret;
}

From source file:org.codice.ddf.spatial.ogc.wfs.transformer.handlebars.HandlebarsWfsFeatureTransformer.java

private String convertToBytes(String value, String unit) {

    BigDecimal resourceSize = new BigDecimal(value);
    resourceSize = resourceSize.setScale(1, BigDecimal.ROUND_HALF_UP);

    switch (unit) {
    case B:/*from w w w.  j a  v a 2  s . co  m*/
        break;
    case KB:
        resourceSize = resourceSize.multiply(new BigDecimal(BYTES_PER_KB));
        break;
    case MB:
        resourceSize = resourceSize.multiply(new BigDecimal(BYTES_PER_MB));
        break;
    case GB:
        resourceSize = resourceSize.multiply(new BigDecimal(BYTES_PER_GB));
        break;
    case TB:
        resourceSize = resourceSize.multiply(new BigDecimal(BYTES_PER_TB));
        break;
    case PB:
        resourceSize = resourceSize.multiply(new BigDecimal(BYTES_PER_PB));
        break;
    default:
        break;
    }

    String resourceSizeAsString = resourceSize.toPlainString();
    LOGGER.debug("resource size in bytes: {}", resourceSizeAsString);
    return resourceSizeAsString;
}

From source file:com.aimluck.eip.project.ProjectTaskSelectData.java

/**
 * ResultData???? <BR>/*from w ww . ja  va 2s .c  o m*/
 *
 * @param record
 *          
 * @return ResultData
 */
@Override
protected Object getResultData(EipTProjectTask record) {
    ProjectTaskResultData data = ProjectUtils.getProjectTaskResultData(record);
    Integer taskId = (int) data.getTaskId().getValue();

    // ?
    int cntChild = ProjectUtils.getCountChildrenTask(taskId);

    // ??2??true
    // ??
    data.setHasChildren(cntChild >= 2);

    // ?
    data.setHasChildrenForForm(cntChild > 0);

    // ?
    int lapsedDays = ProjectUtils.getLapsedDays(ProjectUtils.toString(record.getStartPlanDate()),
            ProjectUtils.toString(Calendar.getInstance().getTime()));
    // 
    int taskDays = ProjectUtils.getLapsedDays(ProjectUtils.toString(record.getStartPlanDate()),
            ProjectUtils.toString(record.getEndPlanDate()));
    data.setPlanTerm(taskDays);

    if (lapsedDays > taskDays) {
        // ???
        lapsedDays = taskDays;
    }

    // ?
    data.setPlanProgressRate(ProjectUtils.getPlanWorkload(lapsedDays, taskDays));

    // 
    List<ProjectTaskMemberResultData> memberList = data.getMemberList();
    BigDecimal workload = BigDecimal.valueOf(0);
    workload = workload.setScale(1);
    for (int i = 0; i < memberList.size(); i++) {
        ProjectTaskMemberResultData member = memberList.get(i);
        workload = workload.add(member.getWorkload());
    }
    data.setWorkload(workload);

    // 
    BigDecimal forecastWorkload = BigDecimal.valueOf(0);
    if (data.getProgressRate().getValue() != 0) {
        forecastWorkload = workload.multiply(BigDecimal.valueOf(100))
                .divide(BigDecimal.valueOf(data.getProgressRate().getValue()), 2, BigDecimal.ROUND_HALF_UP);
    }
    data.setForecastWorkload(forecastWorkload);

    // 
    data.setIndentFlg(indentFlg);

    data.setEditable(ProjectFormUtils.isEditable(taskId, loginUserId));

    return data;
}

From source file:org.efaps.esjp.accounting.transaction.evaluation.DocumentInfo_Base.java

/**
 * Gets the credit sum.// ww  w  . jav a  2  s.c  om
 *
 * @param _parameter Parameter as passed by the eFaps API
 * @return the sum of credit accounts in base currency
 * @throws EFapsException on error
 */
public BigDecimal getCreditSum(final Parameter _parameter) throws EFapsException {
    BigDecimal ret = BigDecimal.ZERO;
    for (final AccountInfo account : getCreditAccounts()) {
        ret = ret.add(account.getAmountRate(_parameter).setScale(2, BigDecimal.ROUND_HALF_UP));
    }
    return ret;
}

From source file:org.egov.ptis.service.es.CollectionIndexElasticSearchService.java

/**
 * Returns the consolidated collections for single day and between the 2 dates
 * // w  w w. jav  a 2 s .  co  m
 * @param collectionDetailsRequest
 * @param fromDate
 * @param toDate
 * @param cityName
 * @return BigDecimal
 */
public BigDecimal getCollectionBetweenDates(CollectionDetailsRequest collectionDetailsRequest, Date fromDate,
        Date toDate, String cityName, String wardName, String fieldName) {
    BoolQueryBuilder boolQuery = prepareWhereClause(collectionDetailsRequest, COLLECTION_INDEX_NAME);
    boolQuery = boolQuery
            .filter(QueryBuilders.rangeQuery(RECEIPT_DATE).gte(DATEFORMATTER_YYYY_MM_DD.format(fromDate))
                    .lte(DATEFORMATTER_YYYY_MM_DD.format(toDate)).includeUpper(false))
            .mustNot(QueryBuilders.matchQuery(STATUS, CANCELLED));
    if (StringUtils.isNotBlank(cityName))
        boolQuery = boolQuery.filter(QueryBuilders.matchQuery(CITY_NAME, cityName));
    else if (StringUtils.isNotBlank(wardName))
        boolQuery = boolQuery.filter(QueryBuilders.matchQuery(REVENUE_WARD, wardName));

    SearchQuery searchQueryColl = new NativeSearchQueryBuilder().withIndices(COLLECTION_INDEX_NAME)
            .withQuery(boolQuery).addAggregation(AggregationBuilders.sum(COLLECTIONTOTAL).field(fieldName))
            .build();

    Aggregations collAggr = elasticsearchTemplate.query(searchQueryColl,
            response -> response.getAggregations());
    Sum aggr = collAggr.get(COLLECTIONTOTAL);
    return BigDecimal.valueOf(aggr.getValue()).setScale(0, BigDecimal.ROUND_HALF_UP);
}

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

public static BigDecimal determinePrice(Product product, Locale locale, String currency) {

    int decimalPlace = 2;

    Map currenciesmap = RefCache.getCurrenciesListWithCodes();
    Currency c = (Currency) currenciesmap.get(currency);

    // prices/*from   ww  w . j a v a  2 s  . c o  m*/
    BigDecimal bdprodprice = product.getProductPrice();
    BigDecimal bddiscountprice = null;

    // discount price
    Special special = product.getSpecial();

    java.util.Date spdate = null;
    java.util.Date spenddate = null;

    if (special != null) {
        bddiscountprice = special.getSpecialNewProductPrice();
        spdate = special.getSpecialDateAvailable();
        spenddate = special.getExpiresDate();
    }

    Date dt = new Date();

    // all other prices
    Set prices = product.getPrices();
    if (prices != null) {
        Iterator pit = prices.iterator();
        while (pit.hasNext()) {
            ProductPrice pprice = (ProductPrice) pit.next();
            pprice.setLocale(locale);

            if (pprice.isDefaultPrice()) {// overwrites default price
                bddiscountprice = null;
                spdate = null;
                spenddate = null;
                bdprodprice = pprice.getProductPriceAmount();
                ProductPriceSpecial ppspecial = pprice.getSpecial();
                if (ppspecial != null) {
                    if (ppspecial.getProductPriceSpecialStartDate() != null
                            && ppspecial.getProductPriceSpecialEndDate() != null

                            && ppspecial.getProductPriceSpecialStartDate().before(new Date(dt.getTime()))
                            && ppspecial.getProductPriceSpecialEndDate().after(new Date(dt.getTime()))) {

                        bddiscountprice = ppspecial.getProductPriceSpecialAmount();

                    } else if (ppspecial.getProductPriceSpecialDurationDays() > -1) {

                        bddiscountprice = ppspecial.getProductPriceSpecialAmount();

                    }
                }
                break;
            }
        }
    }

    double fprodprice = 0;

    if (bdprodprice != null) {
        fprodprice = bdprodprice.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    if (bddiscountprice != null) {

        return bddiscountprice;

    } else {
        return bdprodprice;
    }

}

From source file:org.efaps.esjp.accounting.transaction.evaluation.DocumentInfo_Base.java

/**
 * @return the sum in the rate currency/* w w  w. j a v a 2  s .  co  m*/
 */
public BigDecimal getRateDebitSum() {
    BigDecimal ret = BigDecimal.ZERO;
    for (final AccountInfo account : getDebitAccounts()) {
        ret = ret.add(account.getAmount().setScale(2, BigDecimal.ROUND_HALF_UP));
    }
    return ret;
}

From source file:org.egov.wtms.service.es.WaterChargeCollectionDocService.java

private void prepareCollectionIndexDetails(final WaterChargeDashBoardResponse collectionIndexDetails,
        final BigDecimal totalDemand, final int noOfMonths) {
    final BigDecimal proportionalDemand = totalDemand.divide(BigDecimal.valueOf(12), BigDecimal.ROUND_HALF_UP)
            .multiply(BigDecimal.valueOf(noOfMonths));
    if (proportionalDemand.compareTo(BigDecimal.ZERO) > 0)
        collectionIndexDetails//w w  w .  j  a va  2s  .c o  m
                .setCurrentYearTillDateDmd(proportionalDemand.setScale(0, BigDecimal.ROUND_HALF_UP));
    // performance = (current year tilldate collection * 100)/(proportional
    // demand)
    if (proportionalDemand.compareTo(BigDecimal.ZERO) > 0)
        collectionIndexDetails.setPerformance(
                collectionIndexDetails.getCurrentYearTillDateColl().multiply(WaterTaxConstants.BIGDECIMAL_100)
                        .divide(proportionalDemand, 1, BigDecimal.ROUND_HALF_UP));
    // variance = ((currentYearCollection -
    // lastYearCollection)*100)/lastYearCollection
    BigDecimal variation;
    if (collectionIndexDetails.getLastYearTillDateColl().compareTo(BigDecimal.ZERO) == 0)
        variation = WaterTaxConstants.BIGDECIMAL_100;
    else
        variation = collectionIndexDetails.getCurrentYearTillDateColl()
                .subtract(collectionIndexDetails.getLastYearTillDateColl())
                .multiply(WaterTaxConstants.BIGDECIMAL_100)
                .divide(collectionIndexDetails.getLastYearTillDateColl(), 1, BigDecimal.ROUND_HALF_UP);
    collectionIndexDetails.setLastYearVar(variation);
}

From source file:org.efaps.esjp.accounting.transaction.evaluation.DocumentInfo_Base.java

/**
 * Gets the debit sum./* w  w w  .java  2 s . c o m*/
 *
 * @param _parameter Parameter as passed by the eFaps API
 * @return the sum of all debit accounts in base currency
 * @throws EFapsException on error
 */
public BigDecimal getDebitSum(final Parameter _parameter) throws EFapsException {
    BigDecimal ret = BigDecimal.ZERO;
    for (final AccountInfo account : getDebitAccounts()) {
        ret = ret.add(account.getAmountRate(_parameter).setScale(2, BigDecimal.ROUND_HALF_UP));
    }
    return ret;
}