Example usage for java.math BigDecimal remainder

List of usage examples for java.math BigDecimal remainder

Introduction

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

Prototype

public BigDecimal remainder(BigDecimal divisor) 

Source Link

Document

Returns a BigDecimal whose value is (this % divisor) .

Usage

From source file:org.apache.ofbiz.shipment.thirdparty.usps.UspsServices.java

public static Integer[] convertPoundsToPoundsOunces(BigDecimal decimalPounds) {
    if (decimalPounds == null)
        return null;
    Integer[] poundsOunces = new Integer[2];
    poundsOunces[0] = Integer.valueOf(decimalPounds.setScale(0, BigDecimal.ROUND_FLOOR).toPlainString());
    // (weight % 1) * 16 rounded up to nearest whole number
    poundsOunces[1] = Integer.valueOf(decimalPounds.remainder(BigDecimal.ONE).multiply(new BigDecimal("16"))
            .setScale(0, BigDecimal.ROUND_CEILING).toPlainString());
    return poundsOunces;
}

From source file:org.apache.phoenix.util.DateUtil.java

/**
 * Utility function to convert a {@link BigDecimal} value to {@link Timestamp}.
 *//*from  w w w . ja v a2s . co m*/
public static Timestamp getTimestamp(BigDecimal bd) {
    return DateUtil.getTimestamp(bd.longValue(),
            ((bd.remainder(BigDecimal.ONE).multiply(BigDecimal.valueOf(MILLIS_TO_NANOS_CONVERTOR)))
                    .intValue()));
}

From source file:org.egov.restapi.util.ValidationUtil.java

public Boolean paymentAmountHasDecimals(final BigDecimal amount) {
    return amount.remainder(BigDecimal.ONE).compareTo(BigDecimal.ZERO) == 0 ? false : true;
}

From source file:org.egov.wtms.application.service.collection.ConnectionBillService.java

/**
 * Creates the advance bill details/*from   ww  w . j  a  v  a2 s . c  om*/
 *
 * @param billDetails
 * @param orderMap
 * @param currentInstallmentDemand
 * @param demandDetail
 * @param reason
 * @param installment
 */
private void createAdvanceBillDetails(List<EgBillDetails> billDetails, BigDecimal currentInstallmentDemand,
        EgDemand demand, Billable billable, List<Installment> advanceInstallments,
        Installment dmdDetInstallment) {

    /*
     * Advance will be created with current year second half installment. While fetching advance collection, we will pass
     * current year second half installment
     */
    BigDecimal advanceCollection = demandGenericDAO.getBalanceByDmdMasterCodeInst(demand,
            DEMANDRSN_CODE_ADVANCE, moduleService.getModuleByName(MODULE_NAME), dmdDetInstallment);
    CFinancialYear finYear = financialYearDAO.getFinancialYearByDate(new Date());

    if (advanceCollection.signum() < 0)
        advanceCollection = advanceCollection.abs();
    BigDecimal partiallyCollectedAmount = ZERO;
    if (currentInstallmentDemand.signum() > 0)
        partiallyCollectedAmount = advanceCollection.remainder(currentInstallmentDemand);
    if (currentInstallmentDemand.signum() > 0) {
        Integer noOfAdvancesPaid = advanceCollection.subtract(partiallyCollectedAmount)
                .divide(currentInstallmentDemand).intValue();
        if (LOG.isDebugEnabled())
            LOG.debug("getBilldetails - advanceCollection = {}, noOfAdvancesPaid={}", advanceCollection,
                    noOfAdvancesPaid);

        Installment installment;
        int j;
        if (noOfAdvancesPaid < MAX_ADVANCES_ALLOWED)
            for (int i = noOfAdvancesPaid; i < advanceInstallments.size(); i++) {
                installment = advanceInstallments.get(i);
                EgDemandReason reasonmaster = connectionDemandService
                        .getDemandReasonByCodeAndInstallment(DEMANDRSN_CODE_ADVANCE, installment);
                if (reasonmaster != null) {
                    EgBillDetails billdetail = new EgBillDetails();
                    billdetail.setDrAmount(ZERO);
                    billdetail.setCrAmount(currentInstallmentDemand);

                    billdetail.setGlcode(GLCODE_FOR_ADVANCE);
                    billdetail.setEgDemandReason(reasonmaster);
                    billdetail.setCreateDate(new Date());
                    billdetail.setModifiedDate(new Date());
                    j = billDetails.size() + 1;
                    billdetail.setOrderNo(j);
                    billdetail.setDescription(reasonmaster.getEgDemandReasonMaster().getReasonMaster() + " - "
                            + installment.getDescription());
                    if (billdetail.getDescription().contains(DEMANDRSN_REASON_ADVANCE))
                        billdetail.setPurpose(PURPOSE.ADVANCE_AMOUNT.toString());
                    else if (billdetail.getEgDemandReason().getEgInstallmentMaster().getToDate()
                            .compareTo(finYear.getStartingDate()) < 0)
                        billdetail.setPurpose(PURPOSE.ARREAR_AMOUNT.toString());
                    else if (billdetail.getEgDemandReason().getEgInstallmentMaster().getFromDate()
                            .compareTo(finYear.getStartingDate()) >= 0
                            && billdetail.getEgDemandReason().getEgInstallmentMaster().getToDate()
                                    .compareTo(finYear.getEndingDate()) >= 0)
                        billdetail.setPurpose(PURPOSE.CURRENT_AMOUNT.toString());
                    else
                        billdetail.setPurpose(PURPOSE.OTHERS.toString());
                    billdetail.setFunctionCode(STRING_WCMS_FUCNTION_CODE);
                    billdetail.setAdditionalFlag(0);
                    billDetails.add(billdetail);
                }
            }
    } else if (LOG.isDebugEnabled())
        LOG.debug("getBillDetails - All advances are paid...");
}

From source file:org.kuali.kfs.module.purap.service.impl.PurapAccountingServiceImpl.java

/**
 * @see org.kuali.kfs.module.purap.service.PurapAccountingService#generateAccountDistributionForProrationWithZeroTotal(java.util.List,
 *      java.lang.Integer)//  w  ww  .j a  va  2 s .c  o m
 */
@Override
public List<PurApAccountingLine> generateAccountDistributionForProrationWithZeroTotal(
        PurchasingAccountsPayableDocument purapDoc) {
    String methodName = "generateAccountDistributionForProrationWithZeroTotal()";
    if (LOG.isDebugEnabled()) {
        LOG.debug(methodName + " started");
    }

    List<PurApAccountingLine> accounts = generatePercentSummary(purapDoc);

    // find the total percent and strip trailing zeros
    BigDecimal totalPercentValue = BigDecimal.ZERO;
    for (PurApAccountingLine accountingLine : accounts) {
        BigDecimal linePercent = BigDecimal.ZERO;
        if (ObjectUtils.isNotNull(accountingLine.getAccountLinePercent())) {
            linePercent = accountingLine.getAccountLinePercent();
        }

        totalPercentValue = totalPercentValue.add(linePercent).movePointLeft(2).stripTrailingZeros()
                .movePointRight(2);
    }

    if ((BigDecimal.ZERO.compareTo(totalPercentValue.remainder(ONE_HUNDRED))) != 0) {
        throwRuntimeException(methodName, "Invalid Percent Total of '" + totalPercentValue
                + "' does not allow for account distribution (must be multiple of 100)");
    }

    List newAccounts = new ArrayList();
    BigDecimal logDisplayOnlyTotal = BigDecimal.ZERO;
    BigDecimal percentUsed = BigDecimal.ZERO;
    int accountListSize = accounts.size();
    int i = 0;
    for (PurApAccountingLine accountingLine : accounts) {
        i++;
        BigDecimal percentToUse = BigDecimal.ZERO;
        KualiDecimal amt = KualiDecimal.ZERO;

        if (ObjectUtils.isNotNull(accountingLine.getAmount())) {
            amt = accountingLine.getAmount();
        }

        if (LOG.isDebugEnabled()) {
            LOG.debug(methodName + " " + accountingLine.getChartOfAccountsCode() + "-"
                    + accountingLine.getAccountNumber() + " " + amt + "/" + percentToUse);
        }

        // if it's the last account make up the leftover percent
        BigDecimal acctPercent = BigDecimal.ZERO;
        if (ObjectUtils.isNotNull(accountingLine.getAccountLinePercent())) {
            acctPercent = accountingLine.getAccountLinePercent();
        }

        if ((i != accountListSize) || (accountListSize == 1)) {
            // this account is not the last account or there is only one account
            percentToUse = (acctPercent.divide(totalPercentValue, SCALE, BIG_DECIMAL_ROUNDING_MODE))
                    .multiply(ONE_HUNDRED);
            percentUsed = percentUsed
                    .add(((acctPercent.divide(totalPercentValue, SCALE, BIG_DECIMAL_ROUNDING_MODE)))
                            .multiply(ONE_HUNDRED));
        } else {
            // this account is the last account so we have to makeup whatever is left out of 100
            percentToUse = ONE_HUNDRED.subtract(percentUsed);
        }

        PurApAccountingLine newAccountingLine = accountingLine.createBlankAmountsCopy();
        if (LOG.isDebugEnabled()) {
            LOG.debug(methodName + " pct = " + percentToUse);
        }
        newAccountingLine
                .setAccountLinePercent(percentToUse.setScale(acctPercent.scale(), BIG_DECIMAL_ROUNDING_MODE));
        if (LOG.isDebugEnabled()) {
            LOG.debug(methodName + " adding " + newAccountingLine.getAccountLinePercent());
        }
        newAccounts.add(newAccountingLine);
        logDisplayOnlyTotal = logDisplayOnlyTotal.add(newAccountingLine.getAccountLinePercent());
        if (LOG.isDebugEnabled()) {
            LOG.debug(methodName + " total = " + logDisplayOnlyTotal);
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug(methodName + " ended");
    }
    return newAccounts;
}

From source file:org.nd4j.linalg.util.BigDecimalMath.java

/**
 * Reduce value to the interval [0,2*Pi].
 *
 * @param x the original value/* w ww.j a  va 2 s.  co  m*/
 * @return the value modulo 2*pi in the interval from 0 to 2*pi.
 */
static public BigDecimal mod2pi(BigDecimal x) {
    /* write x= 2*pi*k+r with the precision in r defined by the precision of x and not
     * compromised by the precision of 2*pi, so the ulp of 2*pi*k should match the ulp of x.
     * First getFloat a guess of k to figure out how many digits of 2*pi are needed.
     */
    int k = (int) (0.5 * x.doubleValue() / Math.PI);
    /* want to have err(2*pi*k)< err(x)=0.5*x.ulp, so err(pi) = err(x)/(4k) with two safety digits
     */

    double err2pi;

    if (k != 0) {
        err2pi = 0.25 * Math.abs(x.ulp().doubleValue() / k);
    } else {
        err2pi = 0.5 * Math.abs(x.ulp().doubleValue());
    }
    MathContext mc = new MathContext(2 + err2prec(6.283, err2pi));
    BigDecimal twopi = pi(mc).multiply(new BigDecimal(2));
    /* Delegate the actual operation to the BigDecimal class, which may return
     * a negative value of x was negative .
     */
    BigDecimal res = x.remainder(twopi);

    if (res.compareTo(BigDecimal.ZERO) < 0) {
        res = res.add(twopi);
    }
    /* The actual precision is set by the input value, its absolute value of x.ulp()/2.
     */
    mc = new MathContext(err2prec(res.doubleValue(), x.ulp().doubleValue() / 2.));

    return res.round(mc);

}

From source file:org.nd4j.linalg.util.BigDecimalMath.java

/**
 * Reduce value to the interval [-Pi/2,Pi/2].
 *
 * @param x The original value//from   w  w w .  jav  a  2  s  . c o m
 * @return The value modulo pi, shifted to the interval from -Pi/2 to Pi/2.
 */
static public BigDecimal modpi(BigDecimal x) {
    /* write x= pi*k+r with the precision in r defined by the precision of x and not
     * compromised by the precision of pi, so the ulp of pi*k should match the ulp of x.
     * First getFloat a guess of k to figure out how many digits of pi are needed.
     */
    int k = (int) (x.doubleValue() / Math.PI);
    /* want to have err(pi*k)< err(x)=x.ulp/2, so err(pi) = err(x)/(2k) with two safety digits
     */

    double errpi;

    if (k != 0) {
        errpi = 0.5 * Math.abs(x.ulp().doubleValue() / k);
    } else {
        errpi = 0.5 * Math.abs(x.ulp().doubleValue());
    }
    MathContext mc = new MathContext(2 + err2prec(3.1416, errpi));
    BigDecimal onepi = pi(mc);
    BigDecimal pihalf = onepi.divide(new BigDecimal(2));
    /* Delegate the actual operation to the BigDecimal class, which may return
     * a negative value of x was negative .
     */
    BigDecimal res = x.remainder(onepi);

    if (res.compareTo(pihalf) > 0) {
        res = res.subtract(onepi);
    } else if (res.compareTo(pihalf.negate()) < 0) {
        res = res.add(onepi);
    }
    /* The actual precision is set by the input value, its absolute value of x.ulp()/2.
     */
    mc = new MathContext(err2prec(res.doubleValue(), x.ulp().doubleValue() / 2.));

    return res.round(mc);

}

From source file:org.openhab.binding.denon.internal.DenonConnector.java

private String toDenonValue(BigDecimal percent) {
    // Round to nearest number divisible by 0.5 
    percent = percent.divide(POINTFIVE).setScale(0, RoundingMode.UP).multiply(POINTFIVE)
            .min(connection.getMainVolumeMax()).max(BigDecimal.ZERO);

    String dbString = String.valueOf(percent.intValue());

    if (percent.compareTo(BigDecimal.TEN) == -1) {
        dbString = "0" + dbString;
    }/*from   w w w  .ja  v  a  2 s. c o  m*/
    if (percent.remainder(BigDecimal.ONE).equals(POINTFIVE)) {
        dbString = dbString + "5";
    }

    return dbString;
}

From source file:resources.common.MathUtilities.java

/**
 * Converts seconds to minutes of the hour.
 * @author Waverunner//from  w w w . j  ava 2 s  .co  m
 * @param seconds
 * @return hour(s)
 */
public static int secondsToHourMinutes(int seconds) {
    BigDecimal dec = new BigDecimal(seconds);
    return dec.remainder(new BigDecimal(3600)).divide(new BigDecimal(60), BigDecimal.ROUND_FLOOR).intValue();
}