@Slf4j
public class BigDecimalUtils {
public final static int ROUND_UP = 0;
public final static int ROUND_DOWN = 1;
public final static int ROUND_CEILING = 2;
public final static int ROUND_FLOOR = 3;
public final static int ROUND_HALF_UP = 4;
public final static int ROUND_HALF_DOWN = 5;
public final static int ROUND_HALF_EVEN = 6;
public final static int ROUND_UNNECESSARY = 7;
private static final String[] CN_UPPER_NUMBER = {"零", "壹", "贰", "叁", "肆",
"伍", "陆", "柒", "捌", "玖"};
private static final String[] CN_UPPER_MONETRAY_UNIT = {"分", "角", "元",
"拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾",
"佰", "仟"};
private static final String CN_FULL = "整";
private static final String CN_NEGATIVE = "负";
private static final int MONEY_PRECISION = 2;
private static final String CN_ZEOR_FULL = "零元" + CN_FULL;
private BigDecimalUtils() {
}
public static BigDecimal add(BigDecimal... bigDecimals) {
if (bigDecimals == null || bigDecimals.length < 1) {
return null;
}
return Arrays.stream(bigDecimals).filter(bigDecimal -> bigDecimal != null).reduce(BigDecimal::add).orElse(null);
}
public static BigDecimal add(List<BigDecimal> bigDecimals) {
if (bigDecimals == null || bigDecimals.size() < 1) {
return null;
}
return bigDecimals.stream().filter(bigDecimal -> bigDecimal != null).reduce(BigDecimal::add).orElse(null);
}
public static BigDecimal add(int scale, RoundingMode roundingMode, BigDecimal... bigDecimals) {
BigDecimal addResult = add(bigDecimals);
if (roundingMode == null) {
return addResult;
}
return addResult == null ? null : addResult.setScale(scale, roundingMode);
}
public static BigDecimal add(int scale, RoundingMode roundingMode, List<BigDecimal> bigDecimals) {
BigDecimal addResult = add(bigDecimals);
if (roundingMode == null) {
return addResult;
}
return addResult == null ? null : addResult.setScale(scale, roundingMode);
}
public static BigDecimal add(int scale, int roundingMode, BigDecimal... bigDecimals) {
BigDecimal addResult = add(bigDecimals);
return addResult == null ? null : addResult.setScale(scale, roundingMode);
}
public static BigDecimal subtract(BigDecimal minuend, BigDecimal... subtrahends) {
if (minuend == null) {
return null;
}
BigDecimal totalSubtrahend = add(subtrahends);
if (totalSubtrahend == null) {
return minuend;
}
return minuend.subtract(totalSubtrahend);
}
public static BigDecimal subtract(int scale, int roundingMode, BigDecimal minuend, BigDecimal... subtrahends) {
BigDecimal subtractResult = subtract(minuend, subtrahends);
if (subtractResult == null) {
return null;
}
subtractResult = subtractResult.setScale(scale, roundingMode);
return subtractResult;
}
public static BigDecimal multiply(BigDecimal... bigDecimals) {
if (bigDecimals == null || bigDecimals.length < 1) {
return null;
}
return Arrays.stream(bigDecimals).filter(bigDecimal -> bigDecimal != null).reduce(BigDecimal::multiply).orElse(null);
}
public static BigDecimal multiply(List<BigDecimal> bigDecimals) {
if (bigDecimals == null || bigDecimals.size() < 1) {
return null;
}
return bigDecimals.stream().filter(bigDecimal -> bigDecimal != null).reduce(BigDecimal::multiply).orElse(null);
}
public static BigDecimal multiply(int scale, RoundingMode roundingMode, BigDecimal... bigDecimals) {
BigDecimal multiplyResult = multiply(bigDecimals);
if (roundingMode == null) {
return multiplyResult;
}
return multiplyResult == null ? null : multiplyResult.setScale(scale, roundingMode);
}
public static BigDecimal multiply(int scale, RoundingMode roundingMode, List<BigDecimal> bigDecimals) {
BigDecimal multiplyResult = multiply(bigDecimals);
if (roundingMode == null) {
return multiplyResult;
}
return multiplyResult == null ? null : multiplyResult.setScale(scale, roundingMode);
}
public static BigDecimal multiply(int scale, int roundingMode, BigDecimal... bigDecimals) {
BigDecimal multiplyResult = multiply(bigDecimals);
return multiplyResult == null ? null : multiplyResult.setScale(scale, roundingMode);
}
public static BigDecimal devide(BigDecimal dividend, BigDecimal divisor, int scale, RoundingMode roundingMode) {
if (BigDecimal.ZERO.equals(divisor) || StringUtils.isBlank(divisor.toString())) {
log.error("除数不能为空或者0!");
return null;
}
return dividend.divide(divisor, scale, roundingMode);
}
public static BigDecimal devide(BigDecimal dividend, BigDecimal divisor) {
if (BigDecimal.ZERO.equals(divisor) || StringUtils.isBlank(divisor.toString())) {
log.error("除数不能为空或者0!");
return null;
}
return dividend.divide(divisor);
}
public static BigDecimal addBigDecimal(BigDecimal... bigDecimals) {
BigDecimal sum = BigDecimal.ZERO;
if (bigDecimals == null || bigDecimals.length < 1) {
return sum;
}
for (BigDecimal bigDecimal : bigDecimals) {
if (bigDecimal != null) {
sum = sum.add(new BigDecimal(Double.toString(bigDecimal.doubleValue())));
}
}
return sum;
}
public static BigDecimal subtractBigDecimal(BigDecimal minuend, BigDecimal... subtrahends) {
if (minuend == null) {
minuend = BigDecimal.ZERO;
}
BigDecimal minuendFat = new BigDecimal(Double.toString(minuend.doubleValue()));
if (subtrahends == null || subtrahends.length < 1) {
return minuendFat;
}
for (BigDecimal subtrahend : subtrahends) {
if (subtrahend != null) {
BigDecimal subtrahendFat = new BigDecimal(Double.toString(subtrahend.doubleValue()));
minuendFat = minuendFat.subtract(subtrahendFat);
}
}
return minuendFat;
}
public static String bigDecimalToPercent(BigDecimal decimal) {
return bigDecimalToPercent(decimal, 2);
}
public static String bigDecimalToPercent(BigDecimal decimal, int scale) {
if (decimal == null) {
return "0%";
}
NumberFormat percent = NumberFormat.getPercentInstance();
percent.setMaximumFractionDigits(scale);
return percent.format(keepBigDecimal(decimal, scale + 2).doubleValue());
}
public static BigDecimal keepBigDecimal(BigDecimal decimal, int len) {
if (decimal == null) {
return BigDecimal.ZERO;
}
return decimal.setScale(len, RoundingMode.HALF_UP);
}
public static String numberToCN(BigDecimal numberOfMoney) {
StringBuffer sb = new StringBuffer();
int signum = numberOfMoney.signum();
if (signum == 0) {
return CN_ZEOR_FULL;
}
long number = numberOfMoney.movePointRight(MONEY_PRECISION)
.setScale(0, 4).abs().longValue();
long scale = number % 100;
int numUnit = 0;
int numIndex = 0;
boolean getZero = false;
if (!(scale > 0)) {
numIndex = 2;
number = number / 100;
getZero = true;
}
if ((scale > 0) && (!(scale % 10 > 0))) {
numIndex = 1;
number = number / 10;
getZero = true;
}
int zeroSize = 0;
while (true) {
if (number <= 0) {
break;
}
numUnit = (int) (number % 10);
if (numUnit > 0) {
if ((numIndex == 9) && (zeroSize >= 3)) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[6]);
}
if ((numIndex == 13) && (zeroSize >= 3)) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[10]);
}
sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
sb.insert(0, CN_UPPER_NUMBER[numUnit]);
getZero = false;
zeroSize = 0;
} else {
++zeroSize;
if (!(getZero)) {
sb.insert(0, CN_UPPER_NUMBER[numUnit]);
}
if (numIndex == 2) {
if (number > 0) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
}
} else if (((numIndex - 2) % 4 == 0) && (number % 1000 > 0)) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
}
getZero = true;
}
number = number / 10;
++numIndex;
}
if (signum == -1) {
sb.insert(0, CN_NEGATIVE);
}
if (!(scale > 0)) {
sb.append(CN_FULL);
}
return sb.toString();
}
public static BigDecimal intToBigDecimal(int num) {
BigDecimal a = new BigDecimal(num);
return a;
}
public static BigDecimal longToBigDecimal(Long l) {
if (l == null) {
return BigDecimal.ZERO;
}
BigDecimal bigDecimal = new BigDecimal(l);
return bigDecimal;
}
}