1 /** 2 * Created by My_coder on 2017-07-27. 3 * 加减乘除计算工具类 4 */ 5 public class BigDecimalUtil { 6 private BigDecimalUtil() { 7 } 8 9 /** 10 * 加 11 */ 12 public static BigDecimal add(Double v1, Double v2){ 13 BigDecimal b1 = new BigDecimal(v1.toString()); 14 BigDecimal b2 = new BigDecimal(v2.toString()); 15 return b1.add(b2); 16 } 17 /** 18 * 减 19 */ 20 public static BigDecimal sub(Double v1, Double v2){ 21 BigDecimal b1 = new BigDecimal(v1.toString()); 22 BigDecimal b2 = new BigDecimal(v2.toString()); 23 return b1.subtract(b2); 24 } 25 /** 26 * 乘 27 */ 28 public static BigDecimal mul(Double v1, Double v2){ 29 BigDecimal b1 = new BigDecimal(v1.toString()); 30 BigDecimal b2 = new BigDecimal(v2.toString()); 31 return b1.multiply(b2); 32 } 33 /** 34 * 除 35 */ 36 public static BigDecimal div(Double v1, Double v2){ 37 BigDecimal b1 = new BigDecimal(v1.toString()); 38 BigDecimal b2 = new BigDecimal(v2.toString()); 39 //四舍五入,保留两位小数 40 return b1.divide(b2,2,BigDecimal.ROUND_HALF_DOWN); 41 } 42 }
时间: 2024-11-14 02:30:16