liulingling.177216
2024-08-26 349f1cfc5fa77fbc636d542df0d8050fddec48c2
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
package com.dingzhuo.energy.project.common;
 
/**
 * @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 + "");
    }
 
    /**
     * 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) {
        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) {
        String str = formatDoubleToStr(value, format);
        return toDouble(str);
    }
 
    /**
     * 格式化小数为2位数的小数
     *
     * @param value
     * @return
     */
    public static double formatDouble(double value) {
        return formatDouble(value, 2);
    }
}