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:com.xumpy.thuisadmin.services.implementations.BedragenSrvImpl.java

public static BigDecimal convertComma(String bedrag) {
    if (bedrag.contains(",")) {
        bedrag = bedrag.replace(".", "");
        bedrag = bedrag.replace(",", ".");
    } else {// www . java2s .co  m
        if (bedrag.indexOf(".", bedrag.indexOf(".") + 1) != -1) {
            bedrag = bedrag.replace(".", "");
        }
    }

    NumberFormat nf = NumberFormat.getInstance(new Locale("US"));
    BigDecimal bigDecimalBedrag = new BigDecimal(0);
    try {
        bigDecimalBedrag = new BigDecimal(nf.parse(bedrag).doubleValue());
    } catch (ParseException ex) {
        Logger.getLogger(BedragenSrvImpl.class.getName()).log(Level.SEVERE, null, ex);
    }
    bigDecimalBedrag = bigDecimalBedrag.setScale(2, BigDecimal.ROUND_HALF_UP);

    return bigDecimalBedrag;
}

From source file:org.gradoop.flink.datagen.transactions.foodbroker.config.FoodBrokerConfig.java

/**
 * Returns the average normal value, for default config it is 0.5.
 *
 * @return big decimal value of the average normal value
 *///  ww w  .j  av  a2 s . co  m
private BigDecimal getAvgNormal() {
    return BigDecimal.valueOf((getQualityBad() + getQualityNormal() + getQualityGood()) / 2).setScale(2,
            BigDecimal.ROUND_HALF_UP);
}

From source file:org.egov.adtax.service.notice.AdvertisementNoticeService.java

private Map<String, Object> buildParametersForDemandDetails(
        final AdvertisementPermitDetail advertisementPermitDetail) {
    final Map<String, Object> reportParams = new HashMap<>();

    BigDecimal curntInsAdvertisement = BigDecimal.ZERO;
    BigDecimal curntInsEncrocFee = BigDecimal.ZERO;
    BigDecimal curntInsPenaltyFee = BigDecimal.ZERO;
    BigDecimal curntInsServiceTax = BigDecimal.ZERO;
    BigDecimal curntInsSwachBharatCess = BigDecimal.ZERO;
    BigDecimal curntInsKrishiKalyanCess = BigDecimal.ZERO;
    BigDecimal curntInsTotalTaxableAmt = BigDecimal.ZERO;

    BigDecimal curntInsNetTotal;/*from  w ww  . j a v a 2s  . c o  m*/
    BigDecimal arrInsNetTotal;
    BigDecimal arrInsGrossTotal;
    BigDecimal curntInsGrossTotal;

    BigDecimal arrInsAdvertisement = BigDecimal.ZERO;
    BigDecimal arrInsEncrocFee = BigDecimal.ZERO;
    BigDecimal arrInsPenaltyFee = BigDecimal.ZERO;
    BigDecimal arrInsServiceTax = BigDecimal.ZERO;
    BigDecimal arrInsSwachBharatCess = BigDecimal.ZERO;
    BigDecimal arrInsKrishiKalyanCess = BigDecimal.ZERO;
    BigDecimal arrInsTotalTaxableAmt = BigDecimal.ZERO;
    String previousInstallmentDesc = null;
    Installment currentInstallemnt = advertisementPermitDetail.getAdvertisement().getDemandId()
            .getEgInstallmentMaster();
    List<Installment> previousInstallemnt = advertisementDemandService
            .getPreviousInstallment(currentInstallemnt.getFromDate());
    Map<Installment, BigDecimal> penaltyAmountMap = advertisementPenaltyCalculator
            .getPenaltyByInstallment(advertisementPermitDetail);
    final Map<String, BigDecimal> additionalTaxes = new HashMap<>();
    final List<AdvertisementAdditionalTaxRate> additionalTaxRates = advertisementAdditinalTaxRateService
            .getAllActiveAdditinalTaxRates();
    if (previousInstallemnt.isEmpty()) {
        String currentFinYear = currentInstallemnt.getFinYearRange();
        String[] currentFinYearValues = currentFinYear.split("-");
        Integer from = Integer.parseInt(currentFinYearValues[0]) - 1;
        Integer to = Integer.parseInt(currentFinYearValues[1]) - 1;
        previousInstallmentDesc = from.toString() + "-" + to.toString();
    }
    for (final AdvertisementAdditionalTaxRate taxRates : additionalTaxRates)
        additionalTaxes.put(taxRates.getReasonCode(), taxRates.getPercentage());

    for (final EgDemandDetails demandDtl : advertisementPermitDetail.getAdvertisement().getDemandId()
            .getEgDemandDetails()) {

        if (demandDtl.getBalance().compareTo(BigDecimal.ZERO) > 0) {
            if (currentInstallemnt != null && currentInstallemnt.getDescription()
                    .equals(demandDtl.getEgDemandReason().getEgInstallmentMaster().getDescription())) {
                if (demandDtl.getEgDemandReason().getEgDemandReasonMaster().getCode()
                        .equalsIgnoreCase(DEMANDREASON_ARREAR_ADVERTISEMENTTAX)) {
                    arrInsAdvertisement = arrInsAdvertisement.add(demandDtl.getBalance());
                    arrInsTotalTaxableAmt = arrInsTotalTaxableAmt.add(demandDtl.getBalance());
                } else if (demandDtl.getEgDemandReason().getEgDemandReasonMaster().getCode()
                        .equalsIgnoreCase(DEMANDREASON_ADVERTISEMENTTAX)) {
                    curntInsAdvertisement = curntInsAdvertisement.add(demandDtl.getBalance());
                    reportParams.put("curntInsAdvertisement",
                            curntInsAdvertisement.setScale(2, BigDecimal.ROUND_HALF_EVEN));
                    curntInsTotalTaxableAmt = curntInsTotalTaxableAmt.add(demandDtl.getBalance());
                } else if (demandDtl.getEgDemandReason().getEgDemandReasonMaster().getCode()
                        .equalsIgnoreCase(DEMANDREASON_ENCROCHMENTFEE)) {
                    curntInsEncrocFee = demandDtl.getBalance();
                    reportParams.put("curntInsEncrocFee",
                            curntInsEncrocFee.setScale(2, BigDecimal.ROUND_HALF_EVEN));
                    curntInsTotalTaxableAmt = curntInsTotalTaxableAmt.add(curntInsEncrocFee);
                } else if (demandDtl.getEgDemandReason().getEgDemandReasonMaster().getCode()
                        .equalsIgnoreCase(DEMANDREASON_PENALTY)) {
                    curntInsPenaltyFee = demandDtl.getBalance();
                    reportParams.put("curntInsPenaltyFee",
                            curntInsPenaltyFee.setScale(2, BigDecimal.ROUND_HALF_EVEN));
                }
            } else {
                if (demandDtl.getEgDemandReason().getEgDemandReasonMaster().getCode()
                        .equalsIgnoreCase(DEMANDREASON_ARREAR_ADVERTISEMENTTAX)
                        || demandDtl.getEgDemandReason().getEgDemandReasonMaster().getCode()
                                .equalsIgnoreCase(DEMANDREASON_ADVERTISEMENTTAX)) {
                    arrInsAdvertisement = arrInsAdvertisement.add(demandDtl.getBalance());
                    reportParams.put("arrInsAdvertisement",
                            arrInsAdvertisement.setScale(2, BigDecimal.ROUND_HALF_EVEN));
                    arrInsTotalTaxableAmt = arrInsTotalTaxableAmt.add(demandDtl.getBalance());
                } else if (demandDtl.getEgDemandReason().getEgDemandReasonMaster().getCode()
                        .equalsIgnoreCase(DEMANDREASON_ENCROCHMENTFEE)) {
                    arrInsEncrocFee = arrInsEncrocFee.add(demandDtl.getBalance());
                    reportParams.put("arrInsEncrocFee",
                            arrInsEncrocFee.setScale(2, BigDecimal.ROUND_HALF_EVEN));
                    arrInsTotalTaxableAmt = arrInsTotalTaxableAmt.add(demandDtl.getBalance());
                } else if (demandDtl.getEgDemandReason().getEgDemandReasonMaster().getCode()
                        .equalsIgnoreCase(DEMANDREASON_PENALTY)) {
                    arrInsPenaltyFee = arrInsPenaltyFee.add(demandDtl.getBalance());
                    reportParams.put("arrInsPenaltyFee",
                            arrInsPenaltyFee.setScale(2, BigDecimal.ROUND_HALF_EVEN));
                }
            }
        }
    }

    // Add penalty into reports
    for (final Map.Entry<Installment, BigDecimal> penaltyMap : penaltyAmountMap.entrySet())
        if (currentInstallemnt != null
                && currentInstallemnt.getDescription().equalsIgnoreCase(penaltyMap.getKey().getDescription())) {
            curntInsPenaltyFee = curntInsPenaltyFee.add(penaltyMap.getValue());
            reportParams.put("curntInsPenaltyFee", curntInsPenaltyFee);
        } else {
            arrInsPenaltyFee = arrInsPenaltyFee.add(penaltyMap.getValue());
            reportParams.put("arrInsPenaltyFee", arrInsPenaltyFee);
        }

    for (final Map.Entry<String, BigDecimal> entry : additionalTaxes.entrySet()) {
        if ("Service_Tax".equalsIgnoreCase(entry.getKey())) {
            curntInsServiceTax = calculateAdditionalTaxes(curntInsTotalTaxableAmt, entry.getValue());
            arrInsServiceTax = calculateAdditionalTaxes(arrInsTotalTaxableAmt, entry.getValue());
        } else if ("ADTAX_SB_CESS".equalsIgnoreCase(entry.getKey())) {
            curntInsSwachBharatCess = calculateAdditionalTaxes(curntInsTotalTaxableAmt, entry.getValue());
            arrInsSwachBharatCess = calculateAdditionalTaxes(arrInsTotalTaxableAmt, entry.getValue());
        } else if ("ADTAX_KRISHI_CES".equalsIgnoreCase(entry.getKey())) {
            curntInsKrishiKalyanCess = calculateAdditionalTaxes(curntInsTotalTaxableAmt, entry.getValue());
            arrInsKrishiKalyanCess = calculateAdditionalTaxes(arrInsTotalTaxableAmt, entry.getValue());
        }
    }
    // set additional taxes details and installment
    reportParams.put("currentInstallmentDesc", currentInstallemnt.getDescription());
    reportParams.put("previousInstallmentDesc", previousInstallemnt.isEmpty() ? previousInstallmentDesc
            : previousInstallemnt.get(0).getDescription());
    reportParams.put("curntInsServiceTax", curntInsServiceTax.setScale(2, BigDecimal.ROUND_HALF_EVEN));
    reportParams.put("curntInsKrishiKalyanCess",
            curntInsKrishiKalyanCess.setScale(2, BigDecimal.ROUND_HALF_EVEN));
    reportParams.put("curntInsSwachBharatCess",
            curntInsSwachBharatCess.setScale(2, BigDecimal.ROUND_HALF_EVEN));
    reportParams.put("arrInsServiceTax", arrInsServiceTax.setScale(2, BigDecimal.ROUND_HALF_EVEN));
    reportParams.put("arrInsSwachBharatCess", arrInsSwachBharatCess.setScale(2, BigDecimal.ROUND_HALF_EVEN));
    reportParams.put("arrInsKrishiKalyanCess", arrInsKrishiKalyanCess.setScale(2, BigDecimal.ROUND_HALF_EVEN));

    // sum demand details
    reportParams.put("curntInsTotalTaxableAmt",
            curntInsTotalTaxableAmt.setScale(2, BigDecimal.ROUND_HALF_EVEN));
    reportParams.put("arrInsTotalTaxableAmt", arrInsTotalTaxableAmt.setScale(2, BigDecimal.ROUND_HALF_EVEN));
    curntInsGrossTotal = curntInsTotalTaxableAmt.add(curntInsServiceTax).add(curntInsSwachBharatCess)
            .add(curntInsKrishiKalyanCess);
    reportParams.put("curntInsGrossTotal", curntInsGrossTotal.setScale(2, BigDecimal.ROUND_HALF_EVEN));
    curntInsNetTotal = curntInsGrossTotal.add(curntInsPenaltyFee);
    reportParams.put("curntInsNetTotal", curntInsNetTotal.setScale(2, BigDecimal.ROUND_HALF_EVEN));
    arrInsGrossTotal = arrInsTotalTaxableAmt.add(arrInsServiceTax).add(arrInsSwachBharatCess)
            .add(arrInsKrishiKalyanCess);
    reportParams.put("arrInsGrossTotal", arrInsGrossTotal.setScale(2, BigDecimal.ROUND_HALF_EVEN));
    arrInsNetTotal = arrInsGrossTotal.add(arrInsPenaltyFee);
    reportParams.put("arrInsNetTotal", arrInsNetTotal.setScale(2, BigDecimal.ROUND_HALF_EVEN));
    reportParams.put("advertisementTaxSum",
            curntInsAdvertisement.add(arrInsAdvertisement).setScale(2, BigDecimal.ROUND_HALF_EVEN));
    reportParams.put("encrochmentFeeSum",
            curntInsEncrocFee.add(arrInsEncrocFee).setScale(2, BigDecimal.ROUND_HALF_EVEN));
    reportParams.put("serviceTaxSum",
            curntInsServiceTax.add(arrInsServiceTax).setScale(0, BigDecimal.ROUND_HALF_UP));
    reportParams.put("swachBharatCessSum",
            curntInsSwachBharatCess.add(arrInsSwachBharatCess).setScale(0, BigDecimal.ROUND_HALF_UP));
    reportParams.put("krishiKalyanCessSum",
            curntInsKrishiKalyanCess.add(arrInsKrishiKalyanCess).setScale(0, BigDecimal.ROUND_HALF_UP));
    reportParams.put("penalitySum",
            curntInsPenaltyFee.add(arrInsPenaltyFee).setScale(2, BigDecimal.ROUND_HALF_EVEN));

    reportParams.put("adParticular", advertisementPermitDetail.getAdvertisementParticular());
    reportParams.put("durationOfAdvt", advertisementPermitDetail.getAdvertisementDuration());
    reportParams.put("class", advertisementPermitDetail.getAdvertisement().getRateClass().getDescription());
    reportParams.put("revenueWard", advertisementPermitDetail.getAdvertisement().getWard().getName());
    reportParams.put("electionWard",
            advertisementPermitDetail.getAdvertisement().getElectionWard() != null
                    ? advertisementPermitDetail.getAdvertisement().getElectionWard().getName()
                    : "");
    return reportParams;
}

From source file:net.certiv.authmgr.task.section.core.classifier.BayesPartitionClassifier.java

@SuppressWarnings("unused")
private void report(String category, String partition, String word, double pBi, double pEoB, double wordProb) {
    BigDecimal pBiBD = new BigDecimal(pBi).setScale(5, BigDecimal.ROUND_HALF_UP);
    BigDecimal pEoBiBD = new BigDecimal(pEoB).setScale(8, BigDecimal.ROUND_HALF_UP);
    BigDecimal probBD = new BigDecimal(wordProb).setScale(8, BigDecimal.ROUND_HALF_UP);
    String wordAbbr = StringUtils.abbreviate(word, 13);
    wordAbbr = StringUtils.rightPad(wordAbbr, 14);
    Log.warn(this, "Bayes: " + wordAbbr + pEoBiBD + " (" + probBD + ":" + pBiBD + ")");
}

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

/**
 * A + ( A * B / 100 )/*from  w  w w  .j a  v a  2 s .  c om*/
 *  
 * @param metaA
 * @param dataA
 * @param metaB
 * @param dataB
 * @return
 * @throws KettleValueException
 */
public static Object percent3(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(metaA.getNumber(dataA).doubleValue()
                + (100.0 * metaA.getNumber(dataA).doubleValue() / metaB.getNumber(dataB).doubleValue()));
    case ValueMetaInterface.TYPE_INTEGER:
        return new Long(metaA.getInteger(dataA).longValue()
                + (100 * metaA.getInteger(dataA).longValue() / metaB.getInteger(dataB).longValue()));
    case ValueMetaInterface.TYPE_BIGNUMBER:
        BigDecimal percentTotal = metaA.getBigNumber(dataA).multiply(new BigDecimal(100))
                .divide(metaB.getBigNumber(dataB), BigDecimal.ROUND_HALF_UP);
        return metaA.getBigNumber(dataA).add(percentTotal);

    default:
        throw new KettleValueException("The 'percent3' function only works on numeric data");
    }
}

From source file:org.egov.ptis.domain.service.property.VacancyRemissionService.java

/**
 * Provides 50% rebate on the next installment taxes
 *
 * @param vacancyRemission/*w  ww  . j  a  v a  2s .c  o m*/
 * @param demandInstallment
 * @param effectiveInstallment
 */
private void updateDemandDetailsWithRebate(final VacancyRemission vacancyRemission,
        final Installment demandInstallment, final Installment effectiveInstallment) {
    final Set<Ptdemand> activePropPtDemandSet = vacancyRemission.getBasicProperty().getActiveProperty()
            .getPtDemandSet();
    BigDecimal excess = BigDecimal.ZERO;
    final Set<String> demandReasons = new LinkedHashSet<>(Arrays.asList(DEMANDRSN_CODE_GENERAL_TAX,
            DEMANDRSN_CODE_VACANT_TAX, DEMANDRSN_CODE_EDUCATIONAL_TAX, DEMANDRSN_CODE_LIBRARY_CESS,
            DEMANDRSN_CODE_UNAUTHORIZED_PENALTY, DEMANDRSN_CODE_SCAVENGE_TAX, DEMANDRSN_CODE_WATER_TAX,
            DEMANDRSN_CODE_LIGHT_TAX, DEMANDRSN_CODE_DRAINAGE_TAX));
    Ptdemand currPtDemand = getCurrentPTDemand(demandInstallment, activePropPtDemandSet);
    if (currPtDemand != null) {
        final Set<EgDemandDetails> effectiveInstDemandDetails = propertyService
                .getEgDemandDetailsSetByInstallment(currPtDemand.getEgDemandDetails())
                .get(effectiveInstallment);
        for (final String demandReason : demandReasons) {
            final EgDemandDetails dmdDet = propertyService
                    .getEgDemandDetailsForReason(effectiveInstDemandDetails, demandReason);
            if (dmdDet != null) {
                dmdDet.setAmount(
                        dmdDet.getAmount().divide(new BigDecimal("2")).setScale(0, BigDecimal.ROUND_HALF_UP));
                excess = adjustCollection(excess, dmdDet);
            }
        }
        EgDemandDetails advanceDemandDetails = propertyService
                .getEgDemandDetailsForReason(effectiveInstDemandDetails, ADVANCE_DMD_RSN_CODE);
        updateAdvance(currPtDemand, excess, advanceDemandDetails);
        ptDemandDAO.update(currPtDemand);
    }
}

From source file:org.kuali.kfs.module.ar.document.CustomerCreditMemoDocument.java

public BigDecimal getInvoiceOpenItemQuantity(CustomerCreditMemoDetail customerCreditMemoDetail,
        CustomerInvoiceDetail customerInvoiceDetail) {
    BigDecimal invoiceOpenItemQuantity;
    BigDecimal invoiceItemUnitPrice = customerInvoiceDetail.getInvoiceItemUnitPrice();
    if (ObjectUtils.isNull(invoiceItemUnitPrice) || invoiceItemUnitPrice.equals(BigDecimal.ZERO)) {
        invoiceOpenItemQuantity = BigDecimal.ZERO;
    } else {/*from   w ww  . j  a v a 2 s .  com*/
        KualiDecimal invoiceOpenItemAmount = customerCreditMemoDetail.getInvoiceOpenItemAmount();
        KualiDecimal invoiceOpenItemPretaxAmount = invoiceOpenItemAmount;
        if (getArTaxService().isCustomerInvoiceDetailTaxable(getInvoice(), customerInvoiceDetail)) {
            invoiceOpenItemPretaxAmount = getCustomerInvoiceDetailOpenPretaxAmount(invoiceOpenItemAmount);
        }

        invoiceOpenItemQuantity = invoiceOpenItemPretaxAmount.bigDecimalValue().divide(invoiceItemUnitPrice,
                ArConstants.ITEM_QUANTITY_SCALE, BigDecimal.ROUND_HALF_UP);
    }
    return invoiceOpenItemQuantity;
}

From source file:com.pryzach.suggestions.service.impl.SuggestionServiceImplTest.java

private void performanceTest(long amount) {
    SuggestionService suggestionService = SuggestionFactory.getSuggestionService();
    String[] suggestedWordsArray;
    String[] suggestedNextLettersArray;

    List<String> selectors = new ArrayList<>();

    String wordString;// w  w w .j a  va 2 s. c  o m

    long startTime = new Date().getTime();

    Set<Word> words = new HashSet<>();

    for (int i = 0; i < amount; i++) {

        wordString = RandomStringUtils.randomAlphabetic(randomInt(3, 10));

        words.add(new Word(wordString, randomInt(1, 100)));

        if (randomInt(0, 5) == 2 && wordString.length() > 3) {
            selectors.add(wordString.substring(0, wordString.length() - 3));
        }
    }

    suggestionService.addWords(words);
    words.clear();

    System.out.println(
            "Added to service [" + amount + "] and took [" + (new Date().getTime() - startTime) + " ms]");

    int i = 0;
    startTime = new Date().getTime();
    for (String selector : selectors) {

        suggestedWordsArray = suggestionService.suggest(selector, 10);

        // uncomment below to visually confirm matches
        // if (i % 1000 == 0) System.out.println("Matched [" + selector + "] to [" + result.get("suggestions") + "]");

        Assert.assertTrue(suggestedWordsArray.length >= 1);
        i++;
    }

    System.out.println(
            "Matched: [" + selectors.size() + "] and took [" + (new Date().getTime() - startTime) + " ms] ["
                    + new BigDecimal(((double) new Date().getTime() - startTime) / selectors.size())
                            .setScale(5, BigDecimal.ROUND_HALF_UP).toString()
                    + " ms] per match and ["
                    + Math.round(1000 / (((double) new Date().getTime() - startTime) / selectors.size()))
                    + " words] per second");
}

From source file:org.egov.adtax.service.notice.AdvertisementNoticeService.java

private BigDecimal calculateAdditionalTaxes(BigDecimal curntInsTotalTaxableAmt, final BigDecimal entry) {
    return entry.multiply(curntInsTotalTaxableAmt).divide(BigDecimal.valueOf(100)).setScale(0,
            BigDecimal.ROUND_HALF_UP);
}

From source file:org.eclipse.smarthome.binding.digitalstrom.handler.DeviceHandler.java

private int fromValueToPercent(int value, int max) {
    if (value <= 0 || max <= 0) {
        return 0;
    }//from   w  ww.ja v  a  2s  .c  o m
    return new BigDecimal(value * ((float) 100 / max)).setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
}