admin
2022-03-25 17055fd8d36504b79a5def28f5d4b4740faf012d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package org.yeshi.utils;
 
import java.math.BigDecimal;
 
public class MoneyBigDecimalUtil {
 
    public static BigDecimal add(BigDecimal v1, BigDecimal v2) {
        return v1.add(v2).setScale(2, BigDecimal.ROUND_DOWN);
    }
 
    public static BigDecimal sub(BigDecimal b1, BigDecimal b2) {
        return b1.subtract(b2).setScale(2, BigDecimal.ROUND_DOWN);
    }
 
    public static BigDecimal mul(BigDecimal d1, BigDecimal d2) { // 进行乘法运算
        return d1.multiply(d2).setScale(2, BigDecimal.ROUND_DOWN);
    }
 
    public static BigDecimal mul2(BigDecimal d1, BigDecimal d2) { // 进行乘法运算
        return d1.multiply(d2).setScale(2, BigDecimal.ROUND_DOWN);
    }
    
    public static BigDecimal mul(BigDecimal d1, BigDecimal d2,int scale) { // 进行乘法运算
        return d1.multiply(d2).setScale(scale, BigDecimal.ROUND_DOWN);
    }
 
    public static BigDecimal div(BigDecimal d1, BigDecimal d2) {// 进行除法运算
        return d1.divide(d2, 2, BigDecimal.ROUND_DOWN);
    }
 
    public static BigDecimal divUp(BigDecimal d1, BigDecimal d2) {// 进行除法运算
        return d1.divide(d2, 2, BigDecimal.ROUND_UP);
    }
 
    public static BigDecimal div3(BigDecimal d1, BigDecimal d2) {// 进行除法运算
        return d1.divide(d2, 3, BigDecimal.ROUND_DOWN);
    }
    
    public static BigDecimal div(BigDecimal d1, BigDecimal d2,int scale) {// 进行除法运算
        return d1.divide(d2, scale, BigDecimal.ROUND_DOWN);
    }
 
    public static BigDecimal getWithNoZera(BigDecimal num) {// 进行除法运算
        while (num.toString().endsWith("0") && num.toString().indexOf(".") > -1) {
            num = new BigDecimal(num.toString().substring(0, num.toString().length() - 1));
            return getWithNoZera(num);
        }
        return num;
    }
 
}