吉安感知网项目-后端
xiebin
2026-01-06 d207a86cdf1ab52ef8cb7cd83bad8fceab8038cf
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package org.sxkj.common.utils;
 
import com.alibaba.cloud.commons.lang.StringUtils;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Objects;
import java.util.Optional;
 
public class NoNullUtils {
 
    /**
     * null->0 精确长度2
     *
     * @param data BigDecimal 返回不为null的数
     * @return
     */
    public static BigDecimal bigDecimal(BigDecimal data) {
        return Optional.ofNullable(data).
            orElse(BigDecimal.ZERO).
            setScale(2, BigDecimal.ROUND_HALF_UP);
    }
 
    /**
     * null->0 ,精确长度 4
     *
     * @param data
     * @return
     */
    public static BigDecimal bigDecimalPointFour(BigDecimal data) {
        return Optional.ofNullable(data).
            orElse(BigDecimal.ZERO).
            setScale(4, BigDecimal.ROUND_HALF_UP);
    }
 
    /**
     * null->0
     *
     * @param data
     * @return
     */
    public static Integer integer(Integer data) {
        return Optional.ofNullable(data).orElse(0);
    }
    /**
     * null->0.0
     *
     * @param data
     * @return
     */
    public static Double doubleValue(Double data) {
        return Optional.ofNullable(data).orElse(0.0);
    }
 
    public static Long longValue(Long data) {
        return Optional.ofNullable(data).orElse(0L);
    }
 
    /**
     * str-> inter
     *
     * @param num
     * @return
     */
    public static Integer strDefaultZero(String num) {
        if (StringUtils.isEmpty(num)) {
            return 0;
        }
        Integer result = 0;
        try {
            result = Integer.valueOf(num);
        } catch (Exception e) {
            result = 0;
        }
        return result;
    }
 
    /**
     * 不为空的dd
     * @param d
     * @return
     */
    public static Double getNoNullDouble(Double d,Integer scale) {
        Double result = Optional.ofNullable(d).orElse(0d);
        return new BigDecimal(result).setScale(scale, RoundingMode.HALF_UP).doubleValue();
    }
 
 
    /**
     * 不为空的dd
     * @param d
     * @return
     */
    public static Float getNoNullFloat(Float d,Integer scale) {
        return new BigDecimal(d).setScale(scale, RoundingMode.HALF_UP).floatValue();
    }
 
    /**
     * 格式化的比例
     *
     * @param num      数量
     * @param totalNum 总数量
     * @return 数量/总数量 百分比
     */
    public static String getFormatRate(Integer num, Integer totalNum) {
        String rate = "0";
        if (Objects.nonNull(num) && Objects.nonNull(totalNum) && totalNum > 0 && num > 0) {
            double percentage = (double) num / totalNum * 100;
            DecimalFormat df = new DecimalFormat("0.0"); // 保留 2 位小数
            rate = df.format(percentage);
        }
        return rate;
    }
 
}