Java BigDecimal getPercentage(BigDecimal amount, BigDecimal percent)

Here you can find the source of getPercentage(BigDecimal amount, BigDecimal percent)

Description

get Percentage

License

Open Source License

Declaration

public static BigDecimal getPercentage(BigDecimal amount, BigDecimal percent) 

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 final BigDecimal ONE_HUNDRED = new BigDecimal(100);

    public static BigDecimal getPercentage(BigDecimal amount, BigDecimal percent) {
        return divide(amount.multiply(percent), ONE_HUNDRED);
    }/*from w w w .  j  a  va  2 s  .c o  m*/

    public static BigDecimal getPercentage(BigDecimal amount, BigDecimal percent, Integer scale) {
        return divide(amount.multiply(percent), ONE_HUNDRED, scale);
    }

    public static BigDecimal divide(BigDecimal amount, BigDecimal divisor) {
        return roundBigDecimal(amount.divide(divisor, 2, RoundingMode.HALF_UP));
    }

    public static BigDecimal divide(BigDecimal amount, BigDecimal divisor, int scale) {
        return amount.divide(divisor, scale, RoundingMode.HALF_UP);
    }

    public static BigDecimal multiply(BigDecimal amount, BigDecimal multiplicand, int scale) {
        return roundBigDecimal(amount.multiply(multiplicand), scale);
    }

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

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

    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. getMicroSec(BigDecimal hours)
  2. getNumberAsBigDecimal(Number value)
  3. getNumberOfDecimalPlaces(BigDecimal bigDecimal)
  4. getParamBigDecimal(Map paramMap, String paramName)
  5. getPercent(BigDecimal numerator, BigDecimal denominator)
  6. getPercentageValue(BigDecimal price, double amount)
  7. getProfit(BigDecimal ask, BigDecimal bid, BigDecimal fee)
  8. getProzentWert(BigDecimal zahl, BigDecimal prozentsatz, int nachkommastellen)
  9. getRandomBigDecimal(BigDecimal a, BigDecimal b)