Java BigDecimal Subtract subtract(final BigDecimal start, final BigDecimal... values)

Here you can find the source of subtract(final BigDecimal start, final BigDecimal... values)

Description

Subtract n BigDecimal safely (i.e.

License

Apache License

Declaration

public static BigDecimal subtract(final BigDecimal start, final BigDecimal... values) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.math.BigDecimal;

public class Main {
    /**//  ww  w. ja va  2  s.  c o  m
     * Subtract n BigDecimal safely (i.e. handles nulls), returns 0
     */
    public static BigDecimal subtract(final BigDecimal start, final BigDecimal... values) {
        BigDecimal total = start != null ? start : BigDecimal.ZERO;
        if (values != null) {
            for (final BigDecimal v : values) {
                total = doSubtract(total, v);
            }
        }
        return total;
    }

    /**
     * Subtract 2 BigDecimal safely (i.e. handles nulls) v1 - v2
     */
    private static BigDecimal doSubtract(final BigDecimal v1, final BigDecimal v2) {
        BigDecimal diff = v1;
        if (v1 != null && v2 != null) {
            diff = v1.subtract(v2);
        } else if (v2 != null) {
            diff = v2.negate();
        }
        return diff;
    }

    /**
     * Returns the negate of the value, handles null.
     */
    public static BigDecimal negate(final BigDecimal value) {
        return value != null ? value.negate() : null;
    }
}

Related

  1. subtract(BigDecimal num1, BigDecimal num2)
  2. subtract(BigDecimal number1, BigDecimal number2, int decimalPlaces)
  3. subtract(BigDecimal one, BigDecimal another)
  4. subtract(BigDecimal v1, BigDecimal v2)
  5. subtract(final BigDecimal baseAmount, final BigDecimal amountToSubtract)
  6. subtract(float a, BigDecimal b_bd)
  7. subtract(Vector a, Vector b)
  8. subtract2Abs(BigDecimal aValue1, BigDecimal aValue2)
  9. subtractPercent(BigDecimal price, double amount)