DYL
2025-02-10 816e856344d34b73a13d7db4055de2c66e7cd534
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
package com.zhitan.common.utils;
 
import cn.hutool.core.util.ObjectUtil;
 
/**
 * @Description: 数字工具类
 * @author: yxw
 * @date: 2022年02月07日 15:03
 */
public class DoubleUtil {
    public static double toDouble(String str) {
        double d = 0;
        try {
            d = Double.parseDouble(str);
        } catch (Exception e) {
            d = 0;
        }
        return d;
    }
 
    /**
     * long 转成 double类型
     *
     * @param l
     * @return
     */
    public static double toDouble(long l) {
        return toDouble(l + "");
    }
 
    /**
     * long 转成 double类型
     *
     * @param l
     * @return
     */
    public static double toDouble(Object l) {
        return toDouble(l + "");
    }
 
    /**
     * int 转成 double类型
     *
     * @param i
     * @return
     */
    public static double toDouble(int i) {
        return toDouble(i + "");
    }
 
 
    /**
     * 格式化小数为指定位数的小数字符串
     *
     * @param value
     * @return
     */
    public static String formatDoubleToStr(Double value, int format) {
        if (ObjectUtil.isEmpty(value)) {
            value = 0.00;
        }
        String str = String.format("%." + format + "f", value).toString();
        return str;
    }
 
    /**
     * 格式化小数为指定位数的小数字符串,默认格式化为2位小数
     *
     * @param value
     * @return
     */
    public static String formatDoubleToStr(Double value) {
        return formatDoubleToStr(value, 2);
    }
 
    /**
     * 格式化小数为指定位数的小数
     *
     * @param value
     * @param format
     * @return
     */
    public static double formatDouble(Double value, int format) {
        if (ObjectUtil.isEmpty(value)) {
            return 0D;
        }
        String str = formatDoubleToStr(value, format);
        return toDouble(str);
    }
 
    /**
     * 格式化小数为2位数的小数
     *
     * @param value
     * @return
     */
    public static double formatDouble(Double value) {
        if (ObjectUtil.isEmpty(value)) {
            return 0D;
        }
        return formatDouble(value, 2);
    }
}