package cn.huge.base.common.utils; import org.springframework.util.ObjectUtils; import java.math.BigDecimal; /** * @author zhouxiantao * @date 2024/9/19 5:43 下午 */ public class BigDecimalUtil { //add,subtract,multiply,divide public static Double add(String first,String second){ return toBigDecimal(first).add(toBigDecimal(second)).doubleValue(); } public static Double add(Double first,Double second){ return add(first.toString(),second.toString()); } public static Double subtract(String first,String second){ return toBigDecimal(first).subtract(toBigDecimal(second)).doubleValue(); } public static Double subtract(Double first,Double second){ return subtract(first.toString(),second.toString()); } public static Double multiply(String first,String second){ return toBigDecimal(first).multiply(toBigDecimal(second)).doubleValue(); } public static Double multiply(Double first,Double second){ return multiply(first.toString(),second.toString()); } public static Double multiply(Integer first,Integer second){ return multiply(first.toString(),second.toString()); } /** * @param first * @param second * @param scale 保留小数点位数 * @param roundingMode 进位规则 */ public static Double divide(String first,String second, Integer scale, Integer roundingMode){ if(null==second){ return (double) 0; } if(0==Double.valueOf(second)){ return (double) 0; } return toBigDecimal(first).divide(toBigDecimal(second), scale, roundingMode).doubleValue(); } public static String divideScale(String first, String second, Integer scale, Integer roundingMode){ return toBigDecimal(first).divide(toBigDecimal(second), scale, roundingMode).toString(); } public static Double divideRoundHalf(Integer first, Integer second) { if (null == second) { return (double) 0; } if (0 == second.intValue()) { return (double) 0; } return divide(first.toString(), second.toString(), 2, BigDecimal.ROUND_HALF_UP); } public static Double divide(Double first,Double second, Integer scale, Integer roundingMode){ return divide(first.toString(),second.toString(), scale, roundingMode); } public static Double divide(Integer first,Integer second, Integer scale, Integer roundingMode){ if(null==second){ return (double) 0; } if(0==second.intValue()){ return (double) 0; } return divide(first.toString(),second.toString(), scale, roundingMode); } public static BigDecimal toBigDecimal(String number){ return new BigDecimal(number); } public static BigDecimal toBigDecimal(Double number){ return toBigDecimal(number.toString()); } public static Double getPercent(Double number){ return number * 100; } /** * @param first * @param second * @param scale 保留小数点位数 * 进阶规则:四舍五入 * 去掉小数点后面多余的0 * */ public static Double divideDelZero(Double first,Double second, Integer scale) { if(ObjectUtils.isEmpty(second) || ObjectUtils.isEmpty(first)){ return (double) 0; } if(second == 0 || first == 0){ return (double) 0; } Double number = divide(first, second, scale, BigDecimal.ROUND_HALF_UP); //去掉小数点后面的0 BigDecimal numberD =new BigDecimal(number).setScale(scale, BigDecimal.ROUND_HALF_UP); BigDecimal noZeros = numberD.stripTrailingZeros(); return noZeros.doubleValue(); } /** * @param first * @param second * @param scale 保留小数点位数 * 进阶规则:四舍五入 * 去掉小数点后面多余的0 * */ public static String divideToString(Double first,Double second, Integer scale) { if(ObjectUtils.isEmpty(second) || ObjectUtils.isEmpty(first)){ return "0"; } if(second == 0 || first == 0){ return "0"; } Double number = divide(first, second, scale, BigDecimal.ROUND_HALF_UP); //去掉小数点后面的0 BigDecimal numberD =new BigDecimal(number).setScale(scale, BigDecimal.ROUND_HALF_UP); BigDecimal noZeros = numberD.stripTrailingZeros(); return noZeros.toPlainString(); } /** * @param first * @param second * @param scale 保留小数点位数 * 进阶规则:四舍五入 * 去掉小数点后面多余的0 * */ public static String integerDivideDelZero(Integer first,Integer second, Integer scale) { if(ObjectUtils.isEmpty(second) || ObjectUtils.isEmpty(first)){ return "0"; } if(second == 0 || first == 0){ return "0"; } Double number = divide(first, second, scale, BigDecimal.ROUND_HALF_UP); //去掉小数点后面的0 BigDecimal numberD =new BigDecimal(number).setScale(scale, BigDecimal.ROUND_HALF_UP); BigDecimal noZeros = numberD.stripTrailingZeros(); return noZeros.toPlainString(); } /** * 字符串数字保留一位小数点,并且去掉小数点后面多余的0 * 进阶规则:四舍五入 * */ public static String strDelZero(String number) { BigDecimal numberD =new BigDecimal(number).setScale(1, BigDecimal.ROUND_HALF_UP); //去掉小数点后面的0 BigDecimal noZeros = numberD.stripTrailingZeros(); String result = noZeros.toPlainString(); return result; } /** * double保留对应的小数点,并且去掉小数点后面多余的0 * 进阶规则:四舍五入 * */ public static Double doubleDelZero(Double number,Integer scale) { BigDecimal numberD =new BigDecimal(number).setScale(scale, BigDecimal.ROUND_HALF_UP); //去掉小数点后面的0 BigDecimal noZeros = numberD.stripTrailingZeros(); return noZeros.doubleValue(); } /** * double保留对应的小数点,并且去掉小数点后面多余的0 * 进阶规则:四舍五入 * */ public static String doubleToString(Double number,Integer scale) { if(ObjectUtils.isEmpty(number)) { return "0"; } BigDecimal numberD =new BigDecimal(number).setScale(scale, BigDecimal.ROUND_HALF_UP); //去掉小数点后面的0 BigDecimal noZeros = numberD.stripTrailingZeros(); String result = noZeros.toPlainString(); return result; } /** * float保留对应的小数点,并且去掉小数点后面多余的0 * 进阶规则:四舍五入 * */ public static String floatToString(Float number,Integer scale) { if(ObjectUtils.isEmpty(number)) { return "0"; } BigDecimal numberD =new BigDecimal(number).setScale(scale, BigDecimal.ROUND_HALF_UP); //去掉小数点后面的0 BigDecimal noZeros = numberD.stripTrailingZeros(); String result = noZeros.toPlainString(); return result; } /** * 百分比x100 保留对应的小数点,并且去掉小数点后面多余的0 * 进阶规则:四舍五入 * */ public static String percentDelZero(Double number,Integer scale) { BigDecimal numberD =new BigDecimal(number*100).setScale(scale, BigDecimal.ROUND_HALF_UP); //去掉小数点后面的0 BigDecimal noZeros = numberD.stripTrailingZeros(); String result = noZeros.toPlainString(); return result; } /** * 如果double为null返回0 * */ public static double stringToDouble(String number) { if(ObjectUtils.isEmpty(number) || "null".equals(number)) { return 0d; } return Double.valueOf(number); } /** * 如果Integer为null返回0 * */ public static Integer stringToInteger(String number) { if(ObjectUtils.isEmpty(number)|| "null".equals(number)) { return 0; } return Integer.valueOf(number); } /** * double保留对应的小数点,并且去掉小数点后面多余的0 * 进阶规则:四舍五入 * */ public static String stringDelZero(String number,Integer scale) { if(ObjectUtils.isEmpty(number)) { return "0"; } BigDecimal numberD =new BigDecimal(number).setScale(scale, BigDecimal.ROUND_HALF_UP); //去掉小数点后面的0 BigDecimal noZeros = numberD.stripTrailingZeros(); String result = noZeros.toPlainString(); return result; } /** * double转Integer * 进阶规则:向上取整 * */ public static Integer doubleToIntegerUp(Double number) { BigDecimal numberD =new BigDecimal(number).setScale(0, BigDecimal.ROUND_UP); //去掉小数点后面的0 return numberD.intValue(); } /** * double保留对应的小数点,并且去掉小数点后面多余的0 * 进阶规则:四舍五入 * */ public static Double doubleUp(Double number,Integer scale) { if(ObjectUtils.isEmpty(number)) { return 0d; } BigDecimal numberD =new BigDecimal(number).setScale(scale, BigDecimal.ROUND_HALF_UP); //去掉小数点后面的0 BigDecimal noZeros = numberD.stripTrailingZeros(); return noZeros.doubleValue(); } public static Integer dellNull(Integer number){ if(ObjectUtils.isEmpty(number)) { return 0; } return number; } public static Double dellNull(Double number){ if(ObjectUtils.isEmpty(number)) { return 0d; } return number; } public static void main(String args[]) { int a = 2*100; int b = 37; // String calResult = divideScale("2", "519", 3, BigDecimal.ROUND_HALF_UP); // // System.out.println(integerDivideDelZero(1111, 100, 2)); System.out.println(stringDelZero("202.23000000000002",2)); } }