Java BigDecimal Add add(final BigDecimal baseAmount, final BigDecimal amountToAdd)

Here you can find the source of add(final BigDecimal baseAmount, final BigDecimal amountToAdd)

Description

Adds two amounts rounding to two decimal places.

License

Open Source License

Parameter

Parameter Description
baseAmount the base amount, not null
amountToAdd the amount to add, not null

Return

the total, not null

Declaration

public static BigDecimal add(final BigDecimal baseAmount, final BigDecimal amountToAdd) 

Method Source Code

//package com.java2s;
/**//  ww  w  .  j  a  va  2s . co  m
 * 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;

    /**
     * Adds two amounts rounding to two decimal places.
     * 
     * @param baseAmount  the base amount, not null
     * @param amountToAdd  the amount to add, not null
     * @return the total, not null
     */
    public static BigDecimal add(final BigDecimal baseAmount, final BigDecimal amountToAdd) {
        return rounded(baseAmount).add(rounded(amountToAdd));
    }

    /**
     * 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. add(BigDecimal obj1, BigDecimal obj2)
  2. add(BigDecimal one, BigDecimal another)
  3. add(BigDecimal value1, BigDecimal value2)
  4. add(BigDecimal... operands)
  5. add(BigDecimal[] item1, BigDecimal[] item2)
  6. add(final BigDecimal start, final BigDecimal... values)
  7. add(final BigDecimal v1, final BigDecimal v2)
  8. add(Vector a, Vector b)
  9. add2Abs(BigDecimal aValue1, BigDecimal aValue2)