Java BigDecimal Subtract subtract(BigDecimal amount, BigDecimal subtrahend, int scale)

Here you can find the source of subtract(BigDecimal amount, BigDecimal subtrahend, int scale)

Description

subtract

License

Open Source License

Declaration

public static BigDecimal subtract(BigDecimal amount, BigDecimal subtrahend, int scale) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Main {
    public static BigDecimal subtract(BigDecimal amount, BigDecimal subtrahend, int scale) {
        return roundBigDecimal(amount.subtract(subtrahend), scale);
    }//from   w ww . j  a va  2s.  co m

    public static BigDecimal subtract(BigDecimal... amounts) {
        return subtract(2, amounts);
    }

    public static BigDecimal subtract(int scale, BigDecimal... amounts) {
        BigDecimal result = null;
        for (BigDecimal bigDecimal : amounts) {
            if (bigDecimal != null) {
                result = result == null ? bigDecimal : result.subtract(bigDecimal);
            }
        }
        return roundBigDecimal(result);
    }

    public static BigDecimal roundBigDecimal(BigDecimal num) {
        return roundBigDecimal(num, 2);
    }

    public static BigDecimal roundBigDecimal(BigDecimal num, int scale) {
        if (num != null) {
            return num.setScale(scale, RoundingMode.HALF_UP);
        }
        return num;
    }
}

Related

  1. sub(BigDecimal d1, BigDecimal d2)
  2. sub(BigDecimal v1, BigDecimal v2)
  3. sub(BigDecimal v1, BigDecimal v2)
  4. subtract(BigDecimal a, BigDecimal b)
  5. subtract(BigDecimal a, BigDecimal b)
  6. subtract(BigDecimal aValue1, BigDecimal aValue2)
  7. subtract(BigDecimal bigDecimal, BigDecimal bigDecimal2)
  8. subtract(BigDecimal left, BigDecimal right)
  9. subtract(BigDecimal minuend, BigDecimal subtrahend)