liurunyu
2024-10-19 8456b4e16a9fe4284c45b6d56d7dabeea1cb18e9
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package com.dy.common.util;
 
@SuppressWarnings("unused")
public class NumUtil {
    /**
     * 判断是否是正整数
     * @param str String
     * @return boolean
     */
    public static boolean isPlusIntNumber(String str) {
        if(str == null || str.trim().equals("")){
            return false ;
        }
        int n = 0 ;
        String token = "9876543210";
        for (int i = n; i < str.length(); i++) {
            if (!token.contains(str.substring(i, i + 1))) {
                return false;
            }
        }
        return true;
    }
    /**
     * 判断是否是整数
     * 
     * @param str String
     * @return boolean
     */
    public static boolean isIntNumber(String str) {
        // 判断是否是数字
        if(str == null || str.trim().equals(""))
            return false ;
        
        if(str.startsWith("-")){
            str = str.substring(1) ;
        }
        String token = "9876543210" ;
        for (int i = 0; i < str.length(); i++) {
            if (!token.contains(str.substring(i, i + 1))) {
                return false;
            }
        }
        return true;
    }
    /**
     * 判断是否是整数
     * 
     * @param str String
     * @return boolean
     */
    public static boolean isHex(String str) {
        // 判断是否是数字
        if(str == null || str.trim().equals(""))
            return false ;
        if(str.length()%2 != 0)
            return false ;
        
        String token = "9876543210abcdefABCDEF" ;
        for (int i = 0; i < str.length(); i++) {
            if (!token.contains(str.substring(i, i + 1))) {
                return false;
            }
        }
        return true;
    }
 
    /**
     * 判断是否是浮点数
     * @param str String
     * @return boolean
     */
    public static boolean isDoubleNumber(String str) {
        // 判断是否是数字
        if(str == null || str.trim().equals("")){
            return false ;
        }
        String token = "9876543210.-" ;
        for (int i = 0; i < str.length(); i++) {
            if (!token.contains(str.substring(i, i + 1))) {
                return false;
            } 
        } 
        if(str.startsWith(".") || str.endsWith(".")){
            return false ;
        }else{
            if(str.indexOf('.') != str.lastIndexOf('.')){
                return false ;
            }
        }
        if(str.startsWith("-")){
            return str.indexOf('-') == str.lastIndexOf('-');
        }
        return true;
    }
    /**
     * 判断是否是正浮点数
     * @param str String
     * @return boolean
     */
    public static boolean isPlusDoubleNumber(String str) {
        // 判断是否是数字
        if(str == null || str.trim().equals("")){
            return false ;
        }
        String token = "9876543210." ;
        for (int i = 0; i < str.length(); i++) {
            if (!token.contains(str.substring(i, i + 1))) {
                return false;
            } 
        } 
        if(str.startsWith(".") || str.endsWith(".")){
            return false ;
        }else{
            if(str.indexOf('.') != str.lastIndexOf('.')){
                return false ;
            }
        }
        if(str.startsWith("-")){
            return str.indexOf('-') == str.lastIndexOf('-');
        }
        return true;
    }
    
    /**
     * 浮点数四舍五入
     * @param d 浮点数据
     * @param scale 小数位数
     * @return 四舍五入后的浮点数
     */
    public static Double roundDouble(Double d , int scale){
        if(d == null){
            return null ;
        }
        return Double.valueOf(new java.text.DecimalFormat("##." + "0".repeat(Math.max(0, scale))).format(d)) ;
    }
    
    /**
     * 浮点数四舍五入
     * @param d 浮点数据
     * @param scale 小数位数
     * @return 四舍五入后的浮点数
     */
    public static String roundDoubleStr(Double d , int scale){
        if(d == null){
            return null ;
        }
        return new java.text.DecimalFormat("#0." + "0".repeat(Math.max(0, scale))).format(d);
    }
 
 
    /**
     * 浮点数四舍五入
     * @param d 浮点数据
     * @param scale 小数位数
     * @return 四舍五入后的浮点数
     */
    public static Float roundFloat(Float d , int scale){
        if(d == null){
            return null ;
        }
        // StringBuilder temp = new StringBuilder(".");
        // temp.append("0".repeat(Math.max(0, scale)));
        // return Float.valueOf(new java.text.DecimalFormat(temp.toString()).format(d)) ;
        return Float.valueOf(new java.text.DecimalFormat("." + "0".repeat(Math.max(0, scale))).format(d)) ;
    }
 
    /**
     * 浮点数四舍五入
     * @param d 浮点数据
     * @return 四舍五入后的浮点数
     */
    public static Float roundFloat(Float d){
        if(d == null){
            return null ;
        }
        return Float.valueOf(new java.text.DecimalFormat(".00").format(d)) ;
    }
 
    
    /**
     * 冒泡排序,
     * @param values 要排序的数组
     * @param up true 从小到大  false 从大到小
     * @return 排序后的数组
     */
    public static int[] sort(int[] values , boolean up){
        boolean changed;
        int temp;
        int end = 0 ;
        for (int i = 0; i < values.length; ++i) {
            changed = false ;
            end++ ;
            for (int j = 0; j < values.length - end ; ++j) {
                if(up){
                    if (values[j] > values[j + 1]) {
                        temp = values[j];
                        values[j] = values[j + 1];
                        values[j + 1] = temp;
                        changed = true ;
                    }
                }else{
                    if (values[j] < values[j + 1]) {
                        temp = values[j];
                        values[j] = values[j + 1];
                        values[j + 1] = temp;
                        changed = true ;
                    }
                }
            }
            if(!changed){
                break ;
            }
        }
        return values ;
    }
    /**
     * 把数据前面的0去掉
     * 
     * @param s String
     * @return String
     */
    public String clean0FromStrNum(String s) {
        while (s.charAt(0) == '0' && s.length() > 1) {
            s = s.substring(1);
        }
        return s;
    }
 
    /**
     * 去掉小数点
     * 
     * @param value String
     * @return String
     */
    public String cleanDotNumber(String value) {
        int dot = value.indexOf('.');
        if (dot < 0) {
            return value;
        }
        if (value.length() - dot - 1 > 2) {
            value = value.substring(0, dot);
        }
        return value;
    }
 
}