修正String转Long丢失精度问题
This commit is contained in:
@@ -73,14 +73,37 @@ public class ObjectUtils extends org.apache.commons.lang3.ObjectUtils {
|
|||||||
* 转换为 Long 类型
|
* 转换为 Long 类型
|
||||||
*/
|
*/
|
||||||
public static Long toLong(final Object val) {
|
public static Long toLong(final Object val) {
|
||||||
return toDouble(val).longValue();
|
if (val == null) {
|
||||||
|
return 0L;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
String str = val.toString();
|
||||||
|
if (StringUtils.isBlank(str)) {
|
||||||
|
return 0L;
|
||||||
|
}
|
||||||
|
if (StringUtils.contains(str, "*")) {
|
||||||
|
Long number = null, d = null;
|
||||||
|
for (String s : StringUtils.split(str, "*")) {
|
||||||
|
d = Long.parseLong(StringUtils.trim(s));
|
||||||
|
if (number == null) {
|
||||||
|
number = d;
|
||||||
|
} else {
|
||||||
|
number *= d;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return number;
|
||||||
|
}
|
||||||
|
return Long.parseLong(StringUtils.trim(str));
|
||||||
|
} catch (Exception e) {
|
||||||
|
return 0L;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 转换为 Integer 类型
|
* 转换为 Integer 类型
|
||||||
*/
|
*/
|
||||||
public static Integer toInteger(final Object val) {
|
public static Integer toInteger(final Object val) {
|
||||||
return toDouble(val).intValue();
|
return toLong(val).intValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -153,8 +176,7 @@ public class ObjectUtils extends org.apache.commons.lang3.ObjectUtils {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
byte[] bytes = ObjectUtils.serialize(source);
|
byte[] bytes = ObjectUtils.serialize(source);
|
||||||
Object target = ObjectUtils.unserialize(bytes);
|
return ObjectUtils.unserialize(bytes);
|
||||||
return target;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user