@@ -32,7 +32,7 @@ public static int stringToInt(String string) {
3232 if (Checker .isNumber (string )) {
3333 return Integer .parseInt (string );
3434 }
35- return 0 ;
35+ return - 1 ;
3636 }
3737
3838 public static final Pattern FILE_NAME_PATTERN = Pattern .compile ("([^/\\ \\ :*\" <>|?]+\\ .)*[^/\\ \\ :*\" <>|?]+(\\ ?.*)?$" ,
@@ -42,29 +42,74 @@ public static String formatSize(long size) {
4242 if (size < Values .KB ) {
4343 return size + " B" ;
4444 } else if (size < Values .MB ) {
45- return decimalFormat ((double ) size / Values .KB ) + " KB" ;
45+ return formatDecimal ((double ) size / Values .KB ) + " KB" ;
4646 } else if (size < Values .GB ) {
47- return decimalFormat ((double ) size / Values .MB ) + " MB" ;
47+ return formatDecimal ((double ) size / Values .MB ) + " MB" ;
4848 } else if (size < Values .TB ) {
49- return decimalFormat ((double ) size / Values .GB ) + " GB" ;
49+ return formatDecimal ((double ) size / Values .GB ) + " GB" ;
5050 } else {
51- return decimalFormat ((double ) size / Values .TB ) + " TB" ;
51+ return formatDecimal ((double ) size / Values .TB ) + " TB" ;
5252 }
5353 }
5454
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" );
57102 }
58103
59- public static String decimalFormat (double number , String format ) {
104+ public static String formatDecimal (double number , String format ) {
60105 return new DecimalFormat (format ).format (number );
61106 }
62107
63108 public static String timeStampToString (long time ) {
64109 return new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss" ).format (time );
65110 }
66111
67- public static String jsonFormat (String string ) {
112+ public static String formatJson (String string ) {
68113 String json ;
69114 try {
70115 Gson gson = new GsonBuilder ().setPrettyPrinting ().create ();
0 commit comments