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

private static long add(long xs, long ys) 

Source Link

Usage

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("12.345");
    BigDecimal bg2 = new BigDecimal("23.456");

    System.out.println("Object Value is " + bg1);
    System.out.println("Augend value is " + bg2);

    // create MathContext object with 4 precision
    MathContext mc = new MathContext(4);

    // perform add operation on bg1 with augend bg2 and context mc
    BigDecimal bg3 = bg1.add(bg2, mc);

    System.out.println("Result is " + bg3);
}

From source file:com.idylwood.utils.MathUtils.java

static final double multiplyAndSumSlow(final double[] values, final double scale) {
    BigDecimal ret = new BigDecimal(0);
    for (double x : values)
        ret = ret.add(new BigDecimal(x), MathContext.UNLIMITED);
    return ret.multiply(new BigDecimal(scale), MathContext.UNLIMITED).doubleValue();
}

From source file:com.idylwood.utils.MathUtils.java

final public static double meanArbPrec(final double data[]) {
    BigDecimal mean = new BigDecimal(0);
    for (double x : data)
        mean = mean.add(new BigDecimal(x), MathContext.UNLIMITED);
    mean = mean.divide(new BigDecimal(data.length), MathContext.UNLIMITED);
    return mean.doubleValue();
}

From source file:com.idylwood.utils.MathUtils.java

private final static double varianceSlow(final double[] data) {
    BigDecimal mean = new BigDecimal(0);
    for (double x : data)
        mean = mean.add(new BigDecimal(x), MathContext.UNLIMITED);
    mean = mean.divide(new BigDecimal(data.length), MathContext.UNLIMITED);
    //mean = new BigDecimal(mean(data));
    BigDecimal ret = new BigDecimal(0);
    for (double x : data) {
        //BigDecimal summand = ret.add(new BigDecimal(x),MathContext.UNLIMITED);
        BigDecimal summand = new BigDecimal(x).subtract(mean, MathContext.UNLIMITED);
        ret = ret.add(summand.pow(2));/*from   w  w  w. ja v  a2  s.c om*/
    }
    ret = ret.divide(new BigDecimal(data.length - 1), MathContext.DECIMAL128);
    return ret.doubleValue();
}

From source file:com.idylwood.utils.MathUtils.java

public static final double sumArbitraryPrecision(final double... values) {
    BigDecimal sum = new BigDecimal(0);
    for (int i = values.length; i-- != 0;)
        sum = sum.add(new BigDecimal(values[i]), MathContext.UNLIMITED);
    return sum.doubleValue();
}

From source file:com.idylwood.utils.MathUtils.java

static final double linearCombinationArbitraryPrecision(final double[] x, final double[] y) {
    final int len = Math.min(x.length, y.length);
    //final double [][] ret = new double[len][2];
    BigDecimal ret = new BigDecimal(0);
    for (int i = len; 0 != i--;) {
        BigDecimal product = new BigDecimal(x[i]).multiply(new BigDecimal(y[i]), MathContext.UNLIMITED);
        ret = ret.add(product, MathContext.UNLIMITED);
    }//from  w ww.jav a 2s.co m
    return ret.doubleValue();
}

From source file:com.premiumminds.billy.core.services.builders.impl.GenericInvoiceBuilderImpl.java

protected void validateValues() throws ValidationException {

    GenericInvoiceEntity i = this.getTypeInstance();
    i.setCurrency(Currency.getInstance("EUR")); // FIXME: Hardcoded currency.
                                                // Blocks usage of any other
                                                // currency

    MathContext mc = BillyMathContext.get();

    BigDecimal amountWithTax = BigDecimal.ZERO;
    BigDecimal taxAmount = BigDecimal.ZERO;
    BigDecimal amountWithoutTax = BigDecimal.ZERO;

    for (GenericInvoiceEntry e : this.getTypeInstance().getEntries()) {

        amountWithTax = amountWithTax.add(e.getUnitAmountWithTax().multiply(e.getQuantity(), mc), mc);
        taxAmount = taxAmount.add(e.getUnitTaxAmount().multiply(e.getQuantity(), mc), mc);
        amountWithoutTax = amountWithoutTax.add(e.getUnitAmountWithoutTax().multiply(e.getQuantity(), mc), mc);
        if (e.getCurrency() == null) {
            GenericInvoiceEntryEntity entry = (GenericInvoiceEntryEntity) e;
            entry.setCurrency(i.getCurrency());
            e = entry;/*from   www. j  av a 2  s .  c  o m*/
        } else {
            Validate.isTrue(i.getCurrency().getCurrencyCode().equals(e.getCurrency().getCurrencyCode()));
        }
    }

    i.setAmountWithTax(amountWithTax);
    i.setTaxAmount(taxAmount);
    i.setAmountWithoutTax(amountWithoutTax);

    Validate.isTrue(
            i.getAmountWithTax().subtract(i.getTaxAmount(), mc).setScale(7, mc.getRoundingMode())
                    .compareTo(i.getAmountWithoutTax().setScale(7, mc.getRoundingMode())) == 0,
            "The invoice values are invalid", // TODO message
            i.getAmountWithTax(), i.getAmountWithoutTax(), i.getTaxAmount());

    Validate.isTrue(
            i.getAmountWithTax().compareTo(BigDecimal.ZERO) > 0
                    && i.getAmountWithoutTax().compareTo(BigDecimal.ZERO) >= 0
                    && i.getTaxAmount().compareTo(BigDecimal.ZERO) >= 0,
            "The invoice values are lower than zero", // TODO
            // message
            i.getAmountWithTax(), i.getAmountWithoutTax(), i.getTaxAmount());
}

From source file:com.premiumminds.billy.core.services.builders.impl.GenericInvoiceEntryBuilderImpl.java

protected void validateValues() throws ValidationException {
    MathContext mc = BillyMathContext.get();

    GenericInvoiceEntryEntity e = this.getTypeInstance();

    for (Tax t : e.getProduct().getTaxes()) {
        if (this.daoContext.isSubContext(t.getContext(), this.context)) {
            Date taxDate = e.getTaxPointDate() == null ? new Date() : e.getTaxPointDate();
            if (DateUtils.isSameDay(t.getValidTo(), taxDate) || t.getValidTo().after(taxDate)) {
                e.getTaxes().add(t);/*from   w  w w. ja v  a2s . c o  m*/
            }
        }
    }
    if (e.getTaxes().isEmpty()) {
        throw new ValidationException(
                GenericInvoiceEntryBuilderImpl.LOCALIZER.getString("exception.invalid_taxes"));
    }

    e.setUnitDiscountAmount(BigDecimal.ZERO); // TODO

    if (e.getUnitAmountWithTax() != null) {
        BigDecimal unitAmountWithoutTax = e.getUnitAmountWithTax();
        BigDecimal unitTaxAmount = BigDecimal.ZERO;
        for (Tax t : this.getTypeInstance().getTaxes()) {
            switch (t.getTaxRateType()) {
            case FLAT:
                unitAmountWithoutTax = unitAmountWithoutTax.subtract(t.getValue(), mc);
                unitTaxAmount = unitTaxAmount.add(t.getValue(), mc);
                break;
            case PERCENTAGE:
                unitAmountWithoutTax = e.getUnitAmountWithTax().divide(
                        BigDecimal.ONE.add(t.getPercentageRateValue().divide(new BigDecimal("100"), mc), mc),
                        mc);
                unitTaxAmount = unitTaxAmount.add(e.getUnitAmountWithTax().subtract(unitAmountWithoutTax, mc),
                        mc);

                break;
            default:
                break;
            }
        }
        e.setUnitAmountWithoutTax(unitAmountWithoutTax);
        e.setUnitTaxAmount(unitTaxAmount);

        // Minus discounts
        e.setUnitAmountWithoutTax(unitAmountWithoutTax.subtract(e.getUnitDiscountAmount(), mc));
    } else {
        BigDecimal unitAmountWithTax = e.getUnitAmountWithoutTax();
        BigDecimal unitTaxAmount = BigDecimal.ZERO;

        for (Tax t : this.getTypeInstance().getTaxes()) {
            switch (t.getTaxRateType()) {
            case FLAT:
                unitAmountWithTax = unitAmountWithTax.add(t.getValue(), mc);
                unitTaxAmount = unitTaxAmount.add(t.getValue(), mc);
                break;
            case PERCENTAGE:
                unitTaxAmount = unitTaxAmount.add(e.getUnitAmountWithoutTax()
                        .multiply(t.getPercentageRateValue(), mc).divide(new BigDecimal("100"), mc), mc);
                unitAmountWithTax = unitAmountWithTax.add(unitTaxAmount, mc);
                break;
            default:
                break;
            }
        }

        e.setUnitAmountWithTax(unitAmountWithTax);
        e.setUnitTaxAmount(unitTaxAmount);

    }

    e.setAmountWithTax(this.getTypeInstance().getUnitAmountWithTax().multiply(e.getQuantity(), mc));
    e.setAmountWithoutTax(this.getTypeInstance().getUnitAmountWithoutTax().multiply(e.getQuantity(), mc));
    e.setTaxAmount(this.getTypeInstance().getUnitTaxAmount().multiply(e.getQuantity(), mc));
    e.setDiscountAmount(this.getTypeInstance().getUnitDiscountAmount().multiply(e.getQuantity(), mc));
}

From source file:com.qcadoo.mes.productionCounting.ProductionCountingServiceImpl.java

@Override
public BigDecimal getRegisteredProductValueForOperationProductIn(final Entity operationProduct,
        final BigDecimal planed) {
    BigDecimal value = null;
    Entity toc = operationProduct.getBelongsToField(OperationProductInComponentFields.OPERATION_COMPONENT);
    Entity product = operationProduct.getBelongsToField(OperationProductInComponentFields.PRODUCT);

    List<Entity> tracings = getProductionTrackingDD().find()
            .add(SearchRestrictions.belongsTo(ProductionTrackingFields.TECHNOLOGY_OPERATION_COMPONENT, toc))
            .add(SearchRestrictions.eq(ProductionTrackingFields.STATE,
                    ProductionTrackingStateStringValues.ACCEPTED))
            .list().getEntities();/* ww  w  . j a  v a2  s  . com*/
    for (Entity tracking : tracings) {
        Entity topIN = getTrackingOperationProductInComponentDD().find()
                .add(SearchRestrictions.belongsTo(TrackingOperationProductInComponentFields.PRODUCTION_TRACKING,
                        tracking))
                .add(SearchRestrictions.belongsTo(TrackingOperationProductInComponentFields.PRODUCT, product))
                .setMaxResults(1).uniqueResult();
        if (topIN != null) {
            if (value == null) {
                value = new BigDecimal(0l);
            }
            value = value.add(topIN.getDecimalField(TrackingOperationProductInComponentFields.USED_QUANTITY),
                    numberService.getMathContext());
        }
    }
    if (value != null) {
        value = planed.subtract(value, numberService.getMathContext());
    } else {
        return value;
    }

    if (value.compareTo(new BigDecimal(0l)) == -1) {
        value = new BigDecimal(0l);
    }

    return value;
}

From source file:com.qcadoo.mes.productionCounting.ProductionCountingServiceImpl.java

@Override
public BigDecimal getRegisteredProductValueForOperationProductOut(final Entity operationProduct,
        final BigDecimal planed) {
    BigDecimal value = null;
    Entity toc = operationProduct.getBelongsToField(OperationProductOutComponentFields.OPERATION_COMPONENT);
    Entity product = operationProduct.getBelongsToField(OperationProductOutComponentFields.PRODUCT);

    List<Entity> tracings = getProductionTrackingDD().find()
            .add(SearchRestrictions.belongsTo(ProductionTrackingFields.TECHNOLOGY_OPERATION_COMPONENT, toc))
            .add(SearchRestrictions.eq(ProductionTrackingFields.STATE,
                    ProductionTrackingStateStringValues.ACCEPTED))
            .list().getEntities();//from   www  .  j  av a 2 s  .c  o  m
    for (Entity tracking : tracings) {
        Entity topIN = getTrackingOperationProductOutComponentDD().find()
                .add(SearchRestrictions
                        .belongsTo(TrackingOperationProductOutComponentFields.PRODUCTION_TRACKING, tracking))
                .add(SearchRestrictions.belongsTo(TrackingOperationProductOutComponentFields.PRODUCT, product))
                .setMaxResults(1).uniqueResult();
        if (topIN != null) {
            if (value == null) {
                value = new BigDecimal(0l);
            }
            value = value.add(topIN.getDecimalField(TrackingOperationProductOutComponentFields.USED_QUANTITY),
                    numberService.getMathContext());
        }
    }
    if (value != null) {
        value = planed.subtract(value, numberService.getMathContext());
    } else {
        return value;
    }

    if (value.compareTo(new BigDecimal(0l)) == -1) {
        value = new BigDecimal(0l);
    }

    return value;
}