| 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; | 
|     } | 
|     /** | 
|      * 判断是否为16进制 | 
|      *  | 
|      * @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; | 
|     } | 
|   | 
| } |