admin
2022-04-07 211840b64fa1132d76d6dff6c779e9ba2c0c450f
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
package org.yeshi.utils;
 
import java.text.DecimalFormat;
import java.util.regex.Pattern;
 
public class NumberUtil {
 
    /**
     * 保留2位小数
     * 
     * @param price
     * @return
     */
    public static String get2PointNumber(double price) {
        DecimalFormat decimalFormat = new DecimalFormat("##0.00");//
        String p = decimalFormat.format(price);//
        return p;
    }
 
    /**
     * 保留1位小数
     * 
     * @param price
     * @return
     */
    public static String get1PointNumber(float price) {
        DecimalFormat decimalFormat = new DecimalFormat("##0.0");
        String p = decimalFormat.format(price);
        return p;
    }
 
    /**
     * 保留1位小数
     * 
     * @param price
     * @return
     */
    public static String get1PointNumber(double price) {
        DecimalFormat decimalFormat = new DecimalFormat("##0.0");// 构�?方法的字符格式这里如果小数不�?�?会以0补足.
        String p = decimalFormat.format(price);// format 返回的是字符�?
        return p;
    }
 
    private static String getNumberWithZero(int number) {
        if (number < 10)
            return "0" + number;
        else
            return number + "";
    }
 
    /**
     * 将秒转为时间字符串
     * 
     * @param second
     * @return
     */
    public static String convertSecondToString(int second) {
        if (second < 60) {// 小于1分钟
            return "00:" + getNumberWithZero(second);
        } else if (second < 60 * 60) {// 小于1小时
            int minite = second / 60;
            return getNumberWithZero(minite) + ":" + getNumberWithZero(second % 60);
        } else {
            int hour = second / (60 * 60);
            int minite = (second % (60 * 60)) / 60;
            int se = (second % (60 * 60)) % 60;
            return getNumberWithZero(hour) + ":" + getNumberWithZero(minite) + ":" + getNumberWithZero(se);
        }
    }
 
    /**
     * 判断字符串中是否包含数字
     * 
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
 
        if (str == null || "".equals(str.trim())) {
            return false;
        }
 
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }
 
    /**
     * 去除小数点后面 多余的0
     * 
     * @param s
     * @return
     */
    public static String subZeroAndDot(String s) {
        if (s.indexOf(".") > 0) {
            s = s.replaceAll("0+?$", "");// 去掉多余0
            s = s.replaceAll("[.]$", "");// 如最后一位是.则去0
        }
        return s;
    }
}