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.valco.utility.FacturasUtility.java

public static BigDecimal getTotalImpuestos(Set<Impuestos> impuestos) {
    BigDecimal totalImpuesto = new BigDecimal("0.000000");
    for (Impuestos impuesto : impuestos) {
        totalImpuesto = totalImpuesto.add(impuesto.getImporte()).setScale(6, RoundingMode.HALF_EVEN);
    }//  w  w  w . j av a 2  s .c o m
    return totalImpuesto;
}

From source file:com.intuit.tank.project.JobDetailFormatter.java

protected static BigDecimal estimateCost(int numInstances, BigDecimal costPerHour, long time) {
    BigDecimal cost = BigDecimal.ZERO;
    // calculate the number of machines and the expected run time
    BigDecimal hours = new BigDecimal(Math.max(1, Math.ceil(time / HOURS)));
    cost = cost.add(costPerHour.multiply(new BigDecimal(numInstances)).multiply(hours));
    // dynamoDB costs about 1.5 times the instance cost
    cost = cost.add(cost.multiply(new BigDecimal(1.5D)));
    return cost;/*from ww  w .j av a2 s.  c o  m*/
}

From source file:dk.clanie.money.Money.java

/**
 * Summarize a collection of Money./*from  ww  w .jav a2  s  .c o m*/
 * 
 * All the money must have the same currency.
 * 
 * @param monies
 * @return Money - sum. Null if the collection is empty or null.
 * 
 * @throws IllegalArgumentException
 *             if there are Money with different currencies in the
 *             collection.
 */
public static Money sum(Collection<Money> monies) {
    if (monies == null || monies.size() == 0)
        return null;
    Iterator<Money> iter = monies.iterator();
    Money money = iter.next();
    BigDecimal sum = money.amount;
    Currency currency = money.currency;
    while (iter.hasNext()) {
        money = iter.next();
        if (money.currency != currency) {
            throw new IllegalArgumentException("Amounts must have same currency.");
        }
        sum = sum.add(money.amount);
    }
    return new Money(sum, currency);
}

From source file:com.feilong.core.lang.NumberUtil.java

/**
 * ?./* w ww  .j  a  v a  2  s . c o m*/
 * 
 * <h3>:</h3>
 * 
 * <blockquote>
 * 
 * <pre class="code">
 * NumberUtil.getAddValue(2, 4, 5)              =   11
 * NumberUtil.getAddValue(new BigDecimal(6), 5) =   11
 * </pre>
 * 
 * </blockquote>
 * 
 * @param numbers
 *            the numbers
 * @return  <code>numbers</code> null, {@link NullPointerException}<br>
 *         null, {@link IllegalArgumentException}<br>
 *         ????{@link BigDecimal},?
 * @since 1.5.5
 */
public static BigDecimal getAddValue(Number... numbers) {
    Validate.noNullElements(numbers, "numbers can't be null!");

    BigDecimal sum = BigDecimal.ZERO;
    for (Number number : numbers) {
        sum = sum.add(toBigDecimal(number));
    }
    return sum;
}

From source file:com.whatlookingfor.common.utils.StringUtils.java

/**
 * ???/*from www.  java 2s  . c  o m*/
 *
 * @param v1 
 * @param v2 
 * @return ?
 */

public static double add(double v1, double v2) {
    BigDecimal b1 = new BigDecimal(Double.toString(v1));
    BigDecimal b2 = new BigDecimal(Double.toString(v2));
    return b1.add(b2).doubleValue();
}

From source file:com.prowidesoftware.swift.model.CurrencyAmount.java

/**
 * Creates a currency amount from the sum of all given fields.
 * Will return null if currencies not match.
 * @param fields fields to sum, currency must be the same for all
 * @return total or null if cannot create amount from any field or if not all fields currency match
 *//*from www  .j  a va2  s .c om*/
static CurrencyAmount ofSum(Field... fields) {
    if (fields == null || fields.length == 0) {
        return null;
    }
    BigDecimal total = null;
    String currency = null;
    for (Field field : fields) {
        CurrencyAmount ca = of(field);
        if (ca == null) {
            return null;
        }
        if (total == null) {
            total = ca.getAmount();
            currency = ca.getCurrency();
        } else if (StringUtils.equals(currency, ca.getCurrency())) {
            total = total.add(ca.getAmount());
        } else {
            log.warning("cannot sum amounts with different currencies, expected " + currency + " and found "
                    + ca.getCurrency() + " in field " + field.getName() + ":" + field.getValue());
            return null;
        }
    }
    if (total != null && currency != null) {
        return new CurrencyAmount(currency, total);
    } else {
        return null;
    }
}

From source file:Main.java

/**
 * Subtract one positive Duration from another, larger positive Duration.
 * @param d1 The larger positive duration
 * @param d2 The smaller positive duration
 * @return The difference/*from  w  w w  .j a  va2 s  .c  o m*/
 */
private static Duration subtractSmallerPositiveDurationFromLargerPositiveDuration(Duration d1, Duration d2) {
    BigDecimal s1 = fractionalSeconds(d1);
    BigDecimal s2 = fractionalSeconds(d2);
    BigDecimal extraSeconds = s1.subtract(s2);

    Duration strip1 = stripFractionalSeconds(d1);
    Duration strip2 = stripFractionalSeconds(d2);

    Duration stripResult = strip1.subtract(strip2);

    if (extraSeconds.compareTo(BigDecimal.ZERO) < 0) {
        stripResult = stripResult.subtract(DURATION_1_SECOND);
        extraSeconds = extraSeconds.add(BigDecimal.ONE);
    }

    BigDecimal properSeconds = BigDecimal.valueOf(stripResult.getSeconds()).add(extraSeconds);

    return FACTORY.newDuration(true, BigInteger.valueOf(stripResult.getYears()),
            BigInteger.valueOf(stripResult.getMonths()), BigInteger.valueOf(stripResult.getDays()),
            BigInteger.valueOf(stripResult.getHours()), BigInteger.valueOf(stripResult.getMinutes()),
            properSeconds);
}

From source file:client.Pi.java

/**
 * Compute the value, in radians, of the arctangent of 
 * the inverse of the supplied integer to the specified
 * number of digits after the decimal point.  The value
 * is computed using the power series expansion for the
 * arc tangent://  w  ww.j a v  a2s . c  o  m
 *
 * arctan(x) = x - (x^3)/3 + (x^5)/5 - (x^7)/7 + 
 *     (x^9)/9 ...
 */
public static BigDecimal arctan(int inverseX, int scale) {
    BigDecimal result, numer, term;
    BigDecimal invX = BigDecimal.valueOf(inverseX);
    BigDecimal invX2 = BigDecimal.valueOf(inverseX * inverseX);

    numer = BigDecimal.ONE.divide(invX, scale, roundingMode);

    result = numer;
    int i = 1;
    do {
        numer = numer.divide(invX2, scale, roundingMode);
        int denom = 2 * i + 1;
        term = numer.divide(BigDecimal.valueOf(denom), scale, roundingMode);
        if ((i % 2) != 0) {
            result = result.subtract(term);
        } else {
            result = result.add(term);
        }
        i++;
    } while (term.compareTo(BigDecimal.ZERO) != 0);
    return result;
}

From source file:uk.dsxt.voting.tests.TestDataGenerator.java

private static VoteResult generateVote(String id, HashMap<String, BigDecimal> securities, Voting voting) {
    VoteResult vote = new VoteResult(voting.getId(), id, securities.get(SECURITY));
    for (int j = 0; j < voting.getQuestions().length; j++) {
        String questionId = voting.getQuestions()[j].getId();

        if (voting.getQuestions()[j].isCanSelectMultiple()) {
            BigDecimal totalSum = BigDecimal.ZERO;
            for (int i = 0; i < voting.getQuestions()[j].getAnswers().length; i++) {
                String answerId = voting.getQuestions()[j].getAnswers()[i].getId();
                int amount = randomInt(0, vote.getPacketSize().subtract(totalSum).intValue());
                BigDecimal voteAmount = new BigDecimal(amount);
                totalSum = totalSum.add(voteAmount);
                if (voteAmount.compareTo(BigDecimal.ZERO) > 0)
                    vote.setAnswer(questionId, answerId, voteAmount);
            }/*from w w  w. j  a  v  a2 s . c o  m*/
        } else {
            String answerId = voting.getQuestions()[j].getAnswers()[randomInt(0,
                    voting.getQuestions()[j].getAnswers().length - 1)].getId();
            BigDecimal voteAmount = new BigDecimal(randomInt(0, vote.getPacketSize().intValue()));
            if (voteAmount.compareTo(BigDecimal.ZERO) > 0)
                vote.setAnswer(questionId, answerId, voteAmount);
        }
    }
    return vote;
}

From source file:com.aoindustries.creditcards.TransactionRequest.java

/**
 * Adds-up all amounts into a total amount.
 * @return   amount + tax + shipping + duty
 */// w w w  .  j  a  va  2  s  .  c om
public static BigDecimal getTotalAmount(BigDecimal amount, BigDecimal taxAmount, BigDecimal shippingAmount,
        BigDecimal dutyAmount) {
    if (taxAmount != null)
        amount = amount.add(taxAmount);
    if (shippingAmount != null)
        amount = amount.add(shippingAmount);
    if (dutyAmount != null)
        amount = amount.add(dutyAmount);
    return amount;
}