Java BigDecimal Calculate calculate(BigDecimal number1, String operator, BigDecimal number2, int decimalPlaces)

Here you can find the source of calculate(BigDecimal number1, String operator, BigDecimal number2, int decimalPlaces)

Description

calculate

License

Apache License

Declaration

public static BigDecimal calculate(BigDecimal number1, String operator, BigDecimal number2, int decimalPlaces) 

Method Source Code


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

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

public class Main {
    public static final String ADD = "+";
    public static final String SUBSTRACT = "-";
    public static final String MULTIPLY = "*";
    public static final String DIVIDE = "/";
    private static final MathContext mathContext = MathContext.DECIMAL128;
    private static final RoundingMode roundingMode = RoundingMode.HALF_UP;

    public static BigDecimal calculate(BigDecimal number1, String operator, BigDecimal number2, int decimalPlaces) {

        if (number1 == null) {
            number1 = BigDecimal.ZERO;
        }//from   w ww . ja va 2s  .co m

        if (number2 == null) {
            number2 = BigDecimal.ZERO;
        }

        switch (operator) {
        case ADD:
            return add(number1, number2, decimalPlaces);
        case SUBSTRACT:
            return subtract(number1, number2, decimalPlaces);
        case MULTIPLY:
            return multiply(number1, number2, decimalPlaces);
        case DIVIDE:
            return divide(number1, number2, decimalPlaces);
        default:
            return null;
        }

    }

    public static BigDecimal calculate(BigDecimal number1, String operator, Integer number2, int decimalPlaces) {
        BigDecimal number2Converted = new BigDecimal(Integer.toString(number2));
        return calculate(number1, operator, number2Converted, decimalPlaces);
    }

    public static BigDecimal calculate(BigDecimal number1, String operator, BigDecimal number2) {
        int decimalPlaces = 2;
        return calculate(number1, operator, number2, decimalPlaces);
    }

    public static BigDecimal calculate(BigDecimal number1, String operator, Integer number2) {
        int decimalPlaces = 2;
        return calculate(number1, operator, number2, decimalPlaces);
    }

    private static BigDecimal add(BigDecimal number1, BigDecimal number2, int decimalPlaces) {
        return number1.add(number2, mathContext).setScale(decimalPlaces, roundingMode);
    }

    private static BigDecimal subtract(BigDecimal number1, BigDecimal number2, int decimalPlaces) {
        return number1.subtract(number2, mathContext).setScale(decimalPlaces, roundingMode);
    }

    private static BigDecimal multiply(BigDecimal number1, BigDecimal number2, int decimalPlaces) {
        return number1.multiply(number2, mathContext).setScale(decimalPlaces, roundingMode);
    }

    private static BigDecimal divide(BigDecimal number1, BigDecimal number2, int decimalPlaces) {
        return number1.divide(number2, mathContext).setScale(decimalPlaces, roundingMode);
    }
}

Related

  1. calcBMI(BigDecimal lbs, BigDecimal inches)
  2. calcDistance(final BigDecimal x1, final BigDecimal y1, final BigDecimal x2, final BigDecimal y2)
  3. calcPercentage(int priceInCents, BigDecimal vat)
  4. calculateArithmeticMean(BigDecimal[] values)
  5. calculateDayRate(BigDecimal rate, BigDecimal money)
  6. calculateDeviation(BigDecimal v1, BigDecimal v2)
  7. calculateDivide(BigDecimal numerator, BigDecimal denominator)