cjl
2024-03-28 33b9d2c203a9998272088ecdf43a15dd53669967
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package com.moral.util;
 
import java.io.Serializable;
import java.math.BigDecimal;
 
/**
 * @program: screen
 * @description: double数据计算
 * @author: lizijie
 * @create: 2022-03-22 17:30
 **/
public class DoubleUtils implements Serializable {
 
    private static final long serialVersionUID = -3345205828566485102L;
 
// 默认除法运算精度
 
    private static final Integer DEF_DIV_SCALE = 2;
 
    /**
 
     * 提供精确的加法运算。
 
     * @param value1 被加数
 
     * @param value2 加数
 
     * @return 两个参数的和
 
     */
 
    public static Double add(Number value1, Number value2) {
 
        BigDecimal b1 = new BigDecimal(Double.toString(value1.doubleValue()));
 
        BigDecimal b2 = new BigDecimal(Double.toString(value2.doubleValue()));
 
        return b1.add(b2).doubleValue();
 
    }
 
    /**
 
     * 提供精确的减法运算。
 
     *
 
     * @param value1
 
     * 被减数
 
     * @param value2
 
     * 减数
 
     * @return 两个参数的差
 
     */
 
    public static double sub(Number value1, Number value2) {
 
        BigDecimal b1 = new BigDecimal(Double.toString(value1.doubleValue()));
 
        BigDecimal b2 = new BigDecimal(Double.toString(value2.doubleValue()));
 
        return b1.subtract(b2).doubleValue();
 
    }
 
    /**
 
     * 提供精确的乘法运算。
 
     *
 
     * @param value1
 
     * 被乘数
 
     * @param value2
 
     * 乘数
 
     * @return 两个参数的积
 
     */
 
    public static Double mul(Number value1, Number value2) {
 
        BigDecimal b1 = new BigDecimal(Double.toString(value1.doubleValue()));
 
        BigDecimal b2 = new BigDecimal(Double.toString(value2.doubleValue()));
 
        return b1.multiply(b2).doubleValue();
 
    }
 
    /**
 
     * 提供(相对)精确的除法运算,当发生除不尽的情况时, 精确到小数点以后10位,以后的数字四舍五入。
 
     *
 
     * @param dividend
 
     * 被除数
 
     * @param divisor
 
     * 除数
 
     * @return 两个参数的商
 
     */
 
    public static Double div(Double dividend, Double divisor) {
 
        return div(dividend, divisor, DEF_DIV_SCALE);
 
    }
 
    /**
 
     * 提供(相对)精确的除法运算。 当发生除不尽的情况时,由scale参数指定精度,以后的数字四舍五入。
 
     *
 
     * @param dividend
 
     * 被除数
 
     * @param divisor
 
     * 除数
 
     * @param scale
 
     * 表示表示需要精确到小数点以后几位。
 
     * @return 两个参数的商
 
     */
 
    public static Double div(Double dividend, Double divisor, Integer scale) {
 
        if (scale < 0) {
 
            throw new IllegalArgumentException(
 
                    "The scale must be a positive integer or zero");
 
        }
 
        BigDecimal b1 = new BigDecimal(Double.toString(dividend));
 
        BigDecimal b2 = new BigDecimal(Double.toString(divisor));
 
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
 
    }
 
    /**
 
     * 提供精确的小数位四舍五入处理。
 
     *
 
     * @param value
 
     * 需要四舍五入的数字
 
     * @param scale
 
     * 小数点后保留几位
 
     * @return 四舍五入后的结果
 
     */
 
    public static Double round(Double value, Integer scale) {
 
        if (scale < 0) {
 
            throw new IllegalArgumentException(
 
                    "The scale must be a positive integer or zero");
 
        }
 
        BigDecimal b = new BigDecimal(Double.toString(value));
 
        BigDecimal one = new BigDecimal("1");
 
        return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
 
    }
 
}