public boolean isNumber(String str) {
Pattern pattern = Pattern.compile("(\\d*\\.\\d*)|(\\d*)|(\\d*\\.\\d*\\%)|(\\d*\\%)| (\\-\\d*\\.\\d*)|(\\-\\d*)|(\\-\\d*\\.\\d*\\%)|(\\-\\d*\\%)");
Matcher matcher = pattern.matcher(str);
return matcher.matches();
}
public <T> T toNumber(Object obj, Class<T> clazz) {
return toNumber(obj,null, clazz);
}
public <T> T toNumber(Object obj,Object defaltValue, Class<T> clazz) {
return toNumber(obj,defaltValue,false,false, clazz);
}
public <T> toNumber(Object obj,Object defaltValue,Boolean isperCent, Boolean lessOne, Class<T> clazz) {
if(Objects.isNull(obj)||Strings.isNullOrEmpty(obj.toString)) {
return (T) defaltValue;
}
String str = obj.toString;
str = str.trim();
if(isNumber(str)) {
int per = str.indexOf("%");
if(per == -1&& !isperCent) {
if(Integer.class.equals(clazz)) {
try{
return (T) new Integer(str);
} catch (Exception e) {
if(e instanceof NumberFormatException) {
if(new Long(str) > Integer.MAX_VALUE) {
throw e;
} else {
return (T) new Integer(Double.valueOf(str).intValue())
}
} else {
return (T) defaltValue;
}
}
}else if (Float.class.equals(clazz)) {
return (T) new Float(str);
}else if (BigDecimal.class.equals(clazz)) {
return (T) new BigDecimal(str);
}else if (Double.class.equals(clazz)) {
return (T) new Double(str);
}else if (Long.class.equals(clazz)) {
try{
return (T) new Long(str);
} catch (Exception e) {
if(e instanceof NumberFormatException) {
if(new BigDecimal(str).compareTo(new BigDecimal(Long.MAX_VALUE)) > 0) {
throw e;
} else {
return (T) new Integer(Double.valueOf(str).intValue())
}
} else{
throw e;
}
}
}
} else{
if(per>-1) {
str = str.substring(0,per);
}
if (Float.class.equals(clazz)) {
Float percent = Float.parseFloat(str);
if(percent1< 1&& lessOne) {
return (T) percent
}
percent = percent/100;
return (T) percent;
}else if (BigDecimal.class.equals(clazz)) {
BigDecimal percent = new BigDecimal(str);
if(percent1.compareTo(new BigDecimal(1))< 0&& lessOne) {
return (T) percent
}
percent = percent.devide(new BigDecimal(100));
return (T) percent;
}else if (Double.class.equals(clazz)) {
Double percent = Double.parseFloat(str);
if(percent1< 1&& lessOne) {
return (T) percent
}
percent = percent/100;
return (T) percent;
}
}
}
return (T) defaltValue;
}