import org.springframework.util.StringUtils; import java.math.BigDecimal; public class NumberUtil { /** * 保留小数点后几位 * * @param number * @param precision * @return */ public static String keepPrecision(String number, int precision) { if (StringUtils.isEmpty(number)) { return "0"; } BigDecimal bg = new BigDecimal(number); return bg.setScale(precision, BigDecimal.ROUND_HALF_UP).toPlainString(); } //除法 public static String division(String divisor, Integer dividend, int precision) { BigDecimal bg = new BigDecimal((Double.valueOf(divisor) / Double.valueOf(dividend)) * 100); return bg.setScale(precision, BigDecimal.ROUND_HALF_UP).toPlainString(); } }