Example usage for java.math BigDecimal add

List of usage examples for java.math BigDecimal add

Introduction

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

Prototype

public BigDecimal add(BigDecimal augend) 

Source Link

Document

Returns a BigDecimal whose value is (this + augend) , and whose scale is max(this.scale(), augend.scale()) .

Usage

From source file:com.github.jessemull.microflex.stat.statbigdecimal.GeometricMeanBigDecimalTest.java

/**
 * Corrects any rounding errors due to differences in the implementation of
 * the statistic between the Apache and MicroFlex libraries
 * @param    BigDecimal    the first result
 * @param    BigDecimal    the second result
 * @return                 corrected results
 *//*from w w w .j  a v a 2  s.  c  o  m*/
private static BigDecimal[] correctRoundingErrors(BigDecimal bd1, BigDecimal bd2) {

    BigDecimal[] array = new BigDecimal[2];
    int scale = mc.getPrecision();

    while (!bd1.equals(bd2) && scale > mc.getPrecision() / 4) {

        bd1 = bd1.setScale(scale, RoundingMode.HALF_DOWN);
        bd2 = bd2.setScale(scale, RoundingMode.HALF_DOWN);

        if (bd1.subtract(bd1.ulp()).equals(bd2)) {
            bd1 = bd1.subtract(bd1.ulp());
        }

        if (bd1.add(bd1.ulp()).equals(bd2)) {
            bd1 = bd1.add(bd1.ulp());
        }

        scale--;
    }

    array[0] = bd1;
    array[1] = bd2;

    return array;
}

From source file:org.fineract.module.stellar.TestPaymentInSimpleNetwork.java

@Test
public void overlappingPayments() throws Exception {
    logger.info("overlappingPayments test begin");

    final AccountListener accountListener = new AccountListener(serverAddress, firstTenantId, secondTenantId);

    final BigDecimal transferAmount = BigDecimal.TEN;
    final BigDecimal doubleTransferAmount = transferAmount.add(transferAmount);
    makePayment(firstTenantId, firstTenantApiKey, secondTenantId, ASSET_CODE, transferAmount);
    makePayment(secondTenantId, secondTenantApiKey, firstTenantId, ASSET_CODE, doubleTransferAmount);

    accountListener.waitForCredits(PAY_WAIT,
            creditMatcher(firstTenantId, doubleTransferAmount, ASSET_CODE, secondTenantId),
            creditMatcher(secondTenantId, transferAmount, ASSET_CODE,
                    vaultMatcher(firstTenantId, secondTenantId)),
            creditMatcher(secondTenantId, transferAmount, ASSET_CODE, secondTenantId));

    checkBalance(firstTenantId, firstTenantApiKey, ASSET_CODE, tenantVaultStellarAddress(secondTenantId),
            transferAmount);//from www.ja v a2s .c  o m
    checkBalance(secondTenantId, secondTenantApiKey, ASSET_CODE, tenantVaultStellarAddress(firstTenantId),
            BigDecimal.ZERO);

    //Return balances to zero for next test.
    makePayment(firstTenantId, firstTenantApiKey, secondTenantId, ASSET_CODE, transferAmount);

    accountListener.waitForCredits(PAY_WAIT, creditMatcher(secondTenantId, transferAmount, ASSET_CODE,
            vaultMatcher(firstTenantId, secondTenantId)));
}

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

public static BigDecimal determinePriceNoDiscountWithAttributes(Product product, Collection<Long> attributes,
        Locale locale, String currency) {

    BigDecimal price = product.getProductPrice();

    // 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);/*from   www .j  a v  a  2s.  co  m*/

            if (pprice.isDefaultPrice()) {// overwrites default price
                price = pprice.getProductPriceAmount();
            }
        }
    }

    BigDecimal attributesPrice = null;

    if (attributes != null) {
        Iterator i = attributes.iterator();

        while (i.hasNext()) {
            ProductAttribute attr = (ProductAttribute) i.next();
            if (!attr.isAttributeDisplayOnly() && attr.getOptionValuePrice().longValue() > 0) {
                if (attributesPrice == null) {
                    attributesPrice = new BigDecimal(0);
                }
                attributesPrice = attributesPrice.add(attr.getOptionValuePrice());
            }
        }
    }

    if (attributesPrice != null) {
        price = price.add(attributesPrice);
    }

    return price;

}

From source file:com.acc.facades.process.email.context.NotPickedUpConsignmentCanceledEmailContext.java

protected void calculateRefundAmount() {
    BigDecimal refundAmountValue = BigDecimal.ZERO;
    PriceData totalPrice = null;/* w  ww  .  j a v  a 2s. co m*/
    for (final ConsignmentEntryData consignmentEntry : getConsignment().getEntries()) {
        totalPrice = consignmentEntry.getOrderEntry().getTotalPrice();
        refundAmountValue = refundAmountValue.add(totalPrice.getValue());
    }
    refundAmount = getPriceDataFactory().create(totalPrice.getPriceType(), refundAmountValue,
            totalPrice.getCurrencyIso());
}

From source file:com.opensourcestrategies.financials.accounts.GLAccountInTree.java

/**
 * Gets the debit of this GL account children GL accounts.
 * @return a <code>BigDecimal</code> value
 *//* ww w .  j ava2 s .  com*/
public BigDecimal getDebitOfChildren() {
    BigDecimal debitOfChildren = BigDecimal.ZERO;
    for (GLAccountInTree childAccount : childAccounts) {
        debitOfChildren = debitOfChildren.add(getDebitOfChildrenRec(childAccount));
    }
    return debitOfChildren.setScale(AccountsHelper.decimals, AccountsHelper.rounding);
}

From source file:com.opensourcestrategies.financials.accounts.GLAccountInTree.java

/**
 * Gets the credit of this GL account children GL accounts.
 * @return a <code>BigDecimal</code> value
 *///from   w w w .  jav  a  2 s . co m
public BigDecimal getCreditOfChildren() {
    BigDecimal creditOfChildren = BigDecimal.ZERO;
    for (GLAccountInTree childAccount : childAccounts) {
        creditOfChildren = creditOfChildren.add(getCreditOfChildrenRec(childAccount));
    }
    return creditOfChildren.setScale(AccountsHelper.decimals, AccountsHelper.rounding);
}

From source file:com.opensourcestrategies.financials.accounts.GLAccountInTree.java

/**
 * Gets the balance of this GL account children GL accounts.
 * @return a <code>BigDecimal</code> value
 *//*  w  w w.  j av  a 2s  .  c om*/
public BigDecimal getBalanceOfChildren() {
    BigDecimal balanceOfChildren = BigDecimal.ZERO;
    for (GLAccountInTree childAccount : childAccounts) {
        balanceOfChildren = balanceOfChildren.add(getBalanceOfChildrenRec(childAccount));
    }
    return balanceOfChildren.setScale(AccountsHelper.decimals, AccountsHelper.rounding);
}

From source file:com.ar.dev.tierra.api.dao.impl.FiscalDAOImpl.java

@Override
public void ticket(List<DetalleFactura> detalles) {
    try (PrintWriter ticket = new PrintWriter("command/ticket.200")) {
        DecimalFormat decimalFormat = new DecimalFormat();
        decimalFormat.setMaximumFractionDigits(1);
        ticket.println("@" + (char) 28 + "T" + (char) 28 + "T");
        BigDecimal descuento = new BigDecimal(BigInteger.ZERO);
        for (DetalleFactura detalle : detalles) {
            if (detalle.getDescuentoDetalle() != null) {
                descuento = descuento.add(detalle.getDescuentoDetalle());
            }//  www.  ja  v a 2  s.  com
            String price = null;
            BigDecimal sinIVA = detalle.getProducto().getPrecioVenta().subtract(detalle.getProducto()
                    .getPrecioVenta().multiply(new BigDecimal(17.35)).divide(new BigDecimal(100)));
            price = sinIVA.setScale(4, RoundingMode.HALF_UP).toString();
            ticket.println("B" + (char) 28 /*Abrimos linea*/
                    + detalle.getProducto().getDescripcion() + (char) 28 /*Nombre producto*/
                    + detalle.getCantidadDetalle() + ".0" + (char) 28 /*Cantidad*/
                    + price.replace(",", ".") + (char) 28 /*Precio unitario*/
                    + "21.0" + (char) 28 /*Impuestos IVA*/
                    + "M" + (char) 28 /*Suma monto*/
                    + "0.0" + (char) 28 /*Impuestos internos*/
                    + "0" + (char) 28 /*Parametro display*/
                    + "b");
            /*Cierra de linea*/
        }
        if (!descuento.equals(new BigDecimal(BigInteger.ZERO))) {
            ticket.println("T" + (char) 28 /*Abrimos linea descuento*/
                    + "Descuento: " + (char) 28 /*Texto a mostrar*/
                    + descuento + (char) 28 /*Monto descuento*/
                    + "m" + (char) 28 /*m: descuento, M: aumento*/
                    + "0" + (char) 28 /*parametro display*/
                    + "T");
            /*cierre linea descuento*/
        }
        ticket.println("E");
    } catch (FileNotFoundException ex) {
        Logger.getLogger(FiscalDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.ar.dev.tierra.api.dao.impl.FiscalDAOImpl.java

@Override
public void factura_a(List<DetalleFactura> detalles, Cliente cliente) {
    try (PrintWriter ticket = new PrintWriter("command/factura_a.200")) {
        DecimalFormat decimalFormat = new DecimalFormat();
        decimalFormat.setMaximumFractionDigits(1);
        ticket.println("b" + (char) 28 + cliente.getNombreCliente() + (char) 28 + cliente.getDocumento()
                + (char) 28 + cliente.getResponsabilidadIva() + (char) 28 + cliente.getTipoDocumento()
                + (char) 28 + cliente.getDomicilio());
        ticket.println("@" + (char) 28 + "A" + (char) 28 + "T");
        BigDecimal descuento = new BigDecimal(BigInteger.ZERO);
        for (DetalleFactura detalle : detalles) {
            if (detalle.getDescuentoDetalle() != null) {
                descuento = descuento.add(detalle.getDescuentoDetalle());
            }//from ww w  . j av a2 s. co  m
            String price = null;
            BigDecimal sinIVA = detalle.getProducto().getPrecioVenta().subtract(detalle.getProducto()
                    .getPrecioVenta().multiply(new BigDecimal(17.35)).divide(new BigDecimal(100)));
            price = sinIVA.setScale(4, RoundingMode.HALF_UP).toString();
            ticket.println("B" + (char) 28 /*Abrimos linea*/
                    + detalle.getProducto().getDescripcion() + (char) 28 /*Nombre producto*/
                    + detalle.getCantidadDetalle() + ".0" + (char) 28 /*Cantidad*/
                    + price.replace(",", ".") + (char) 28 /*Precio unitario*/
                    + "21.0" + (char) 28 /*Impuestos IVA*/
                    + "M" + (char) 28 /*Suma monto*/
                    + "0.0" + (char) 28 /*Impuestos internos*/
                    + "0" + (char) 28 /*Parametro display*/
                    + "b");
            /*Cierra de linea*/
        }
        if (!descuento.equals(new BigDecimal(BigInteger.ZERO))) {
            ticket.println("T" + (char) 28 /*Abrimos linea descuento*/
                    + "Descuento: " + (char) 28 /*Texto a mostrar*/
                    + descuento + (char) 28 /*Monto descuento*/
                    + "m" + (char) 28 /*m: descuento, M: aumento*/
                    + "0" + (char) 28 /*parametro display*/
                    + "T");
            /*cierre linea descuento*/
        }
        ticket.println("E");
    } catch (FileNotFoundException ex) {
        Logger.getLogger(FiscalDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.ar.dev.tierra.api.dao.impl.FiscalDAOImpl.java

@Override
public void factura_b(List<DetalleFactura> detalles, Cliente cliente) {
    try (PrintWriter ticket = new PrintWriter("command/factura_b.200")) {
        DecimalFormat decimalFormat = new DecimalFormat();
        decimalFormat.setMaximumFractionDigits(1);
        ticket.println("b" + (char) 28 + cliente.getNombreCliente() + (char) 28 + cliente.getDocumento()
                + (char) 28 + cliente.getResponsabilidadIva() + (char) 28 + cliente.getTipoDocumento()
                + (char) 28 + cliente.getDomicilio());
        ticket.println("@" + (char) 28 + "B" + (char) 28 + "T");
        BigDecimal descuento = new BigDecimal(BigInteger.ZERO);
        for (DetalleFactura detalle : detalles) {
            if (detalle.getDescuentoDetalle() != null) {
                descuento = descuento.add(detalle.getDescuentoDetalle());
            }//from   w w  w .ja va2 s  .c o m
            String price = null;
            BigDecimal sinIVA = detalle.getProducto().getPrecioVenta().subtract(detalle.getProducto()
                    .getPrecioVenta().multiply(new BigDecimal(17.35)).divide(new BigDecimal(100)));
            price = sinIVA.setScale(4, RoundingMode.HALF_UP).toString();
            ticket.println("B" + (char) 28 /*Abrimos linea*/
                    + detalle.getProducto().getDescripcion() + (char) 28 /*Nombre producto*/
                    + detalle.getCantidadDetalle() + ".0" + (char) 28 /*Cantidad*/
                    + price.replace(",", ".") + (char) 28 /*Precio unitario*/
                    + "21.0" + (char) 28 /*Impuestos IVA*/
                    + "M" + (char) 28 /*Suma monto*/
                    + "0.0" + (char) 28 /*Impuestos internos*/
                    + "0" + (char) 28 /*Parametro display*/
                    + "b");
            /*Cierra de linea*/
        }
        if (!descuento.equals(new BigDecimal(BigInteger.ZERO))) {
            ticket.println("T" + (char) 28 /*Abrimos linea descuento*/
                    + "Descuento: " + (char) 28 /*Texto a mostrar*/
                    + descuento + (char) 28 /*Monto descuento*/
                    + "m" + (char) 28 /*m: descuento, M: aumento*/
                    + "0" + (char) 28 /*parametro display*/
                    + "T");
            /*cierre linea descuento*/
        }
        ticket.println("E");
    } catch (FileNotFoundException ex) {
        Logger.getLogger(FiscalDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
    }
}