Java BigDecimal Subtract subtract(final BigDecimal baseAmount, final BigDecimal amountToSubtract)

Here you can find the source of subtract(final BigDecimal baseAmount, final BigDecimal amountToSubtract)

Description

Subtract one amount from another rounding to two decimal places.

License

Open Source License

Parameter

Parameter Description
baseAmount the amount to subtract from, not null
amountToSubtract the amount to subtract, not null

Return

the subtraction result, not null

Declaration

public static BigDecimal subtract(final BigDecimal baseAmount, final BigDecimal amountToSubtract) 

Method Source Code

//package com.java2s;
/**/*w  ww .  jav a 2 s.  c om*/
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */

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

public class Main {
    /**
     * The number of decimals to retain.
     */
    public static final int DECIMALS = 2;
    /**
     * The rounding mode.
     */
    public static final RoundingMode ROUNDING_MODE = RoundingMode.HALF_EVEN;

    /**
     * Subtract one amount from another rounding to two decimal places.
     * 
     * @param baseAmount  the amount to subtract from, not null
     * @param amountToSubtract  the amount to subtract, not null
     * @return the subtraction result, not null
     */
    public static BigDecimal subtract(final BigDecimal baseAmount, final BigDecimal amountToSubtract) {
        return rounded(baseAmount).subtract(rounded(amountToSubtract));
    }

    /**
     * Rounds an amount to two decimal places.
     * 
     * @param amount  the amount to round, not null
     * @return the rounded amount, not null
     */
    public static BigDecimal rounded(BigDecimal amount) {
        return amount.setScale(DECIMALS, ROUNDING_MODE);
    }
}

Related

  1. subtract(BigDecimal minuend, BigDecimal subtrahend)
  2. subtract(BigDecimal num1, BigDecimal num2)
  3. subtract(BigDecimal number1, BigDecimal number2, int decimalPlaces)
  4. subtract(BigDecimal one, BigDecimal another)
  5. subtract(BigDecimal v1, BigDecimal v2)
  6. subtract(final BigDecimal start, final BigDecimal... values)
  7. subtract(float a, BigDecimal b_bd)
  8. subtract(Vector a, Vector b)
  9. subtract2Abs(BigDecimal aValue1, BigDecimal aValue2)