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:Main.java

public static BigDecimal getSeconds(XMLGregorianCalendar x) {
    BigDecimal fractional = x.getFractionalSecond();
    if (fractional == null)
        fractional = BigDecimal.ZERO;

    BigDecimal whole = BigDecimal.valueOf(x.getSecond());

    return whole.add(fractional);
}

From source file:Money.java

/**
*Null elements in the argument array will not cause things to blow up with a NullPointerException.
*Instead they will be ignored, because we foresee some circumstances in which a caller
*might have a sparsely populated array it wants summed up.  Note that call this class's
*add(Money) method one at a time does not, as of this writing, share this behavior.  Instead
*it will just blow up.//from   www . j  a  v a2s  . co  m
*/
public static Money add(Money[] moneys) {
    //Attempt to save on object creation by adding up the BigDecimal
    //delegates.  So rather than creating a Money and a BigDecimal
    //with each element of the sum, we're just creating a BigDecimal.
    BigDecimal total = new BigDecimal("0");
    for (int i = 0; i < moneys.length; i++) {
        if (moneys[i] != null) {
            total = total.add(moneys[i].getBigDecimalValue());
        }
    }
    return new Money(total);
}

From source file:utilities.itext.Turnover.java

private static Paragraph createTablesWithDetails(ArrayList<PaymentPosition> data) throws DocumentException {
    Paragraph paragraph = new Paragraph();

    PdfPTable expense = new PdfPTable(2);
    float[] colWidths = { 5f, 1f };
    expense.setWidths(colWidths);// w  w w.  j av a2s. co  m

    int lastPaymentID = 0;
    for (PaymentPosition pp : data) {
        if (lastPaymentID != pp.getPayment().getId()) {
            //new Payment
            Paragraph p1 = new Paragraph(pp.getPayment().getPayee().getName());
            PdfPCell c1 = new PdfPCell(p1);

            //add next cell with price
            //calc price
            BigDecimal totalPrice = new BigDecimal(BigInteger.ZERO);
            for (PaymentPosition payPos : data) {
                if (payPos.getPayment().getId() == pp.getPayment().getId()) {
                    totalPrice = totalPrice.add(payPos.getTotalPrice());
                }
            }
            Paragraph p2 = new Paragraph(totalPrice.toPlainString());
            PdfPCell c2 = new PdfPCell(p2);

            c1.setBorder(Rectangle.NO_BORDER);
            c1.setBorderWidthTop(0.5f);
            c2.setBorder(Rectangle.NO_BORDER);
            c2.setBorderWidthTop(0.5f);

            expense.addCell(c1);
            expense.addCell(c2);
        }

        //payment position
        Paragraph p1 = new Paragraph("            " + pp.getQuantity() + "x: " + pp.getArticle().getName());
        PdfPCell c1 = new PdfPCell(p1);

        //add next cell with price
        Paragraph p2 = new Paragraph(pp.getTotalPriceAsString());
        PdfPCell c2 = new PdfPCell(p2);

        c1.setBorder(Rectangle.NO_BORDER);
        c2.setBorder(Rectangle.NO_BORDER);

        expense.addCell(c1);
        expense.addCell(c2);

        lastPaymentID = pp.getPayment().getId();
    }

    paragraph.add(expense);

    paragraph.setSpacingBefore(10f);

    return paragraph;
}

From source file:Util.java

public static BigDecimal[] quadratic(BigDecimal a, BigDecimal b, BigDecimal c, RoundingMode rounding) {
    // ax^2 + bx + c = 0
    BigDecimal part1 = b.negate();
    BigDecimal part2 = sqrt(b.multiply(b).subtract(BigDecimalFOUR.multiply(a).multiply(c)), rounding);
    BigDecimal part3 = BigDecimalTWO.multiply(a);
    BigDecimal[] toRet = new BigDecimal[2];
    toRet[0] = (part1.subtract(part2)).divide(part3, RoundingMode.CEILING);
    toRet[1] = (part1.add(part2)).divide(part3, RoundingMode.CEILING);
    return toRet;
}

From source file:Main.java

/** Returns Map with total, squaredTotal, count, average, stdDev, maximum; fieldName field in Maps must have type BigDecimal;
 * if count of non-null fields is less than 2 returns null as cannot calculate a standard deviation */
public static Map<String, BigDecimal> stdDevMaxFromMapField(List<Map<String, Object>> dataList,
        String fieldName, BigDecimal stdDevMultiplier) {
    BigDecimal total = BigDecimal.ZERO;
    BigDecimal squaredTotal = BigDecimal.ZERO;
    int count = 0;
    for (Map<String, Object> dataMap : dataList) {
        if (dataMap == null)
            continue;
        BigDecimal value = (BigDecimal) dataMap.get(fieldName);
        if (value == null)
            continue;
        total = total.add(value);
        squaredTotal = squaredTotal.add(value.multiply(value));
        count++;//w ww.  j a  va2s . c o m
    }
    if (count < 2)
        return null;

    BigDecimal countBd = new BigDecimal(count);
    BigDecimal average = total.divide(countBd, BigDecimal.ROUND_HALF_UP);
    double totalDouble = total.doubleValue();
    BigDecimal stdDev = new BigDecimal(Math
            .sqrt(Math.abs(squaredTotal.doubleValue() - ((totalDouble * totalDouble) / count)) / (count - 1)));

    Map<String, BigDecimal> retMap = new HashMap<>(6);
    retMap.put("total", total);
    retMap.put("squaredTotal", squaredTotal);
    retMap.put("count", countBd);
    retMap.put("average", average);
    retMap.put("stdDev", stdDev);

    if (stdDevMultiplier != null)
        retMap.put("maximum", average.add(stdDev.multiply(stdDevMultiplier)));

    return retMap;
}

From source file:Main.java

/**
 * Add two positive Duration objects./*w  w  w .  j  av  a  2s .co m*/
 * @param d1 The first Duration.
 * @param d2 The second Duration.
 * @return The sum of the two durations.
 */
private static Duration addPositiveDurations(Duration d1, Duration d2) {
    BigDecimal s1 = fractionalSeconds(d1);
    BigDecimal s2 = fractionalSeconds(d2);
    BigDecimal extraSeconds = s1.add(s2);

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

    Duration stripResult = strip1.add(strip2);

    if (extraSeconds.compareTo(BigDecimal.ONE) >= 0) {
        stripResult = stripResult.add(DURATION_1_SECOND);
        extraSeconds = extraSeconds.subtract(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:Main.java

public static Duration subtract(XMLGregorianCalendar x1, XMLGregorianCalendar x2) {
    boolean positive = x1.compare(x2) >= 0;

    if (!positive) {
        XMLGregorianCalendar temp = x1;
        x1 = x2;/*  ww  w .  j a v a  2 s . c  om*/
        x2 = temp;
    }

    BigDecimal s1 = getSeconds(x1);
    BigDecimal s2 = getSeconds(x2);
    BigDecimal seconds = s1.subtract(s2);
    if (seconds.compareTo(BigDecimal.ZERO) < 0)
        seconds = seconds.add(BigDecimal.valueOf(60));

    GregorianCalendar g1 = x1.toGregorianCalendar();
    GregorianCalendar g2 = x2.toGregorianCalendar();

    int year = 0;
    for (int f : reverseFields) {
        if (f == Calendar.YEAR) {
            int year1 = g1.get(f);
            int year2 = g2.get(f);
            year = year1 - year2;
        } else {
            subtractField(g1, g2, f);
        }
    }

    return FACTORY.newDuration(positive, BigInteger.valueOf(year), BigInteger.valueOf(g1.get(Calendar.MONTH)),
            BigInteger.valueOf(g1.get(Calendar.DAY_OF_MONTH) - 1),
            BigInteger.valueOf(g1.get(Calendar.HOUR_OF_DAY)), BigInteger.valueOf(g1.get(Calendar.MINUTE)),
            seconds);
}

From source file:BigDSqrt.java

public static BigDecimal sqrt(BigDecimal n, int s) {
    BigDecimal TWO = BigDecimal.valueOf(2);

    // Obtain the first approximation
    BigDecimal x = n.divide(BigDecimal.valueOf(3), s, BigDecimal.ROUND_DOWN);
    BigDecimal lastX = BigDecimal.valueOf(0);

    // Proceed through 50 iterations
    for (int i = 0; i < 50; i++) {
        x = n.add(x.multiply(x)).divide(x.multiply(TWO), s, BigDecimal.ROUND_DOWN);
        if (x.compareTo(lastX) == 0)
            break;
        lastX = x;//from   w  ww  . ja va 2 s .co  m
    }
    return x;
}

From source file:gov.nih.nci.calims2.ui.administration.customerservice.serviceitem.ServiceItemHelper.java

/**
 * Gets the total for the given service.
 * //from  ww  w  . jav a  2s .  c  om
 * @param service The service for which the calculation must be done
 * @return the total for the given service. or null if any serviceItem is not computable
 */
public static BigDecimal getServiceTotal(Service service) {
    BigDecimal subtotal = new BigDecimal("0");
    Set<ServiceItem> serviceItems = service.getServiceItemCollection();
    if (serviceItems != null) {
        for (ServiceItem serviceItem : serviceItems) {
            BigDecimal itemSubTotal = getsubTotalValue(serviceItem);
            if (itemSubTotal == null) {
                return null;
            }
            subtotal = subtotal.add(ServiceItemHelper.getsubTotalValue(serviceItem));
        }

    }
    return subtotal;
}

From source file:com.willetinc.hadoop.mapreduce.dynamodb.BinarySplitter.java

/**
 * Return a BigDecimal representation of byte[] array suitable for use in a
 * numerically-sorting order./*from  w  w  w . j  a  v  a 2s  .  c om*/
 */
static BigDecimal byteArrayToBigDecimal(byte[] array, int maxBytes) {
    BigDecimal result = BigDecimal.ZERO;
    BigDecimal curPlace = ONE_PLACE; // start with 1/16 to compute the
    // first digit.

    int len = Math.min(array.length, maxBytes);

    for (int i = 0; i < len; i++) {
        byte codePoint = array[i];
        result = result.add(tryDivide(new BigDecimal(codePoint), curPlace));
        // advance to the next less significant place. e.g., 1/(16^2) for
        // the second char.
        curPlace = curPlace.multiply(ONE_PLACE);
    }

    return result;
}