@@ -32,7 +32,7 @@ public static int stringToInt(String string) {
32
32
if (Checker .isNumber (string )) {
33
33
return Integer .parseInt (string );
34
34
}
35
- return 0 ;
35
+ return - 1 ;
36
36
}
37
37
38
38
public static final Pattern FILE_NAME_PATTERN = Pattern .compile ("([^/\\ \\ :*\" <>|?]+\\ .)*[^/\\ \\ :*\" <>|?]+(\\ ?.*)?$" ,
@@ -42,29 +42,74 @@ public static String formatSize(long size) {
42
42
if (size < Values .KB ) {
43
43
return size + " B" ;
44
44
} else if (size < Values .MB ) {
45
- return decimalFormat ((double ) size / Values .KB ) + " KB" ;
45
+ return formatDecimal ((double ) size / Values .KB ) + " KB" ;
46
46
} else if (size < Values .GB ) {
47
- return decimalFormat ((double ) size / Values .MB ) + " MB" ;
47
+ return formatDecimal ((double ) size / Values .MB ) + " MB" ;
48
48
} else if (size < Values .TB ) {
49
- return decimalFormat ((double ) size / Values .GB ) + " GB" ;
49
+ return formatDecimal ((double ) size / Values .GB ) + " GB" ;
50
50
} else {
51
- return decimalFormat ((double ) size / Values .TB ) + " TB" ;
51
+ return formatDecimal ((double ) size / Values .TB ) + " TB" ;
52
52
}
53
53
}
54
54
55
- public static String decimalFormat (double number ) {
56
- return decimalFormat (number , "#0.00" );
55
+ /**
56
+ * 将格式化后的大小转换成long型
57
+ *
58
+ * @param size
59
+ * 格式:34.12 MB
60
+ * @return long
61
+ */
62
+ public static long sizeToLong (String size ) {
63
+ if (Checker .isNotEmpty (size )) {
64
+ String num = size .split (" " )[0 ];
65
+ double result = 0 ;
66
+ if (size .contains ("TB" )) {
67
+ result = stringToDouble (num ) * Values .TB ;
68
+ } else if (size .contains ("GB" )) {
69
+ result = stringToDouble (num ) * Values .GB ;
70
+ } else if (size .contains ("MB" )) {
71
+ result = stringToDouble (num ) * Values .MB ;
72
+ } else if (size .contains ("KB" )) {
73
+ result = stringToDouble (num ) * Values .KB ;
74
+ } else {
75
+ result = stringToDouble (num );
76
+ }
77
+ return (long ) result ;
78
+ }
79
+ return -1 ;
80
+ }
81
+
82
+ public static double stringToDouble (String s ) {
83
+ if (Checker .isDecimal (s )) {
84
+ return Double .parseDouble (s );
85
+ }
86
+ return -1 ;
87
+ }
88
+
89
+ public static long stringToLong (String s ) {
90
+ if (Checker .isNumber (s )) {
91
+ return Long .parseLong (s );
92
+ }
93
+ return -1 ;
94
+ }
95
+
96
+ public static String customFormatDecimal (double number , String format ) {
97
+ return formatDecimal (number , format );
98
+ }
99
+
100
+ public static String formatDecimal (double number ) {
101
+ return formatDecimal (number , "#0.00" );
57
102
}
58
103
59
- public static String decimalFormat (double number , String format ) {
104
+ public static String formatDecimal (double number , String format ) {
60
105
return new DecimalFormat (format ).format (number );
61
106
}
62
107
63
108
public static String timeStampToString (long time ) {
64
109
return new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss" ).format (time );
65
110
}
66
111
67
- public static String jsonFormat (String string ) {
112
+ public static String formatJson (String string ) {
68
113
String json ;
69
114
try {
70
115
Gson gson = new GsonBuilder ().setPrettyPrinting ().create ();
0 commit comments