public classStatUtils {private static Pattern linePattern = Pattern.compile("_(\\w)");public static String chgListStrToInStr(String searchName, ListsearchIds) {if (!StringUtil.isBlank(searchName) && !CollectionUtil.isEmpty(searchIds)) {
StringBuffer retStr= newStringBuffer();for(int i = 0; i < searchIds.size(); ++i) {if (i % 1000 == 0) {if (i == 0) {
retStr.append(searchName).append(" in (");
}else{
retStr.append(") or ").append(searchName).append(" in (");
}
}else{
retStr.append(" , ");
}
retStr.append("'").append((String)searchIds.get(i)).append("'");
}
retStr.append(")");returnretStr.toString();
}else{return "";
}
}/*** 下划线转驼峰
*@paramstr
*@return
*/
public staticString lineToHump(String str){
str=str.toLowerCase();
Matcher matcher=linePattern.matcher(str);
StringBuffer sb= newStringBuffer();while(matcher.find()){
matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
}
matcher.appendTail(sb);returnsb.toString();
}/*** 获取四舍五入
*@paramvalue
*@return
*/
public static String roundHalfUp(double value, intscaleNum) {
String formatStr= addZeroForNum("0.", (scaleNum+2));
DecimalFormat df= newDecimalFormat(formatStr);double result = newBigDecimal(String.valueOf(value)).setScale(scaleNum, BigDecimal.ROUND_HALF_UP).doubleValue();returndf.format(result);
}public static String addZeroForNum(String str, intstrLength) {int strLen =str.length();
StringBuffer sb= null;while (strLen
sb= newStringBuffer();
sb.append(str).append("0");//右补0
str =sb.toString();
strLen=str.length();
}returnstr;
}public staticObject setFieldValueByFieldName(Object o, String fieldName, Object fieldValue) {try{
Field f=o.getClass().getDeclaredField(fieldName);
f.setAccessible(true);try{
f.set(o, fieldValue);
}catch(IllegalAccessException e) {
e.printStackTrace();
}
}catch(NoSuchFieldException e) {
e.printStackTrace();
}returno;
}/*** 根据属性名获取属性值
*
*@paramfieldName
*@paramobject
*@return
*/
public staticObject getFieldValueByFieldName(String fieldName, Object object) {try{
Field field=object.getClass().getDeclaredField(fieldName);//设置对象的访问权限,保证对private的属性的访问
field.setAccessible(true);returnfield.get(object);
}catch(Exception e) {
e.printStackTrace();return null;
}
}public static Map objectToMap(Object obj) throwsException {if(obj == null){return null;
}
Map map = new HashMap();
Field[] declaredFields=obj.getClass().getDeclaredFields();for(Field field : declaredFields) {
field.setAccessible(true);
map.put(field.getName(), field.get(obj));
}returnmap;
}public static voidmain(String[] args) {
System.out.println(roundHalfUp(0.2000606, 6));
}
}