Browse Source

修改类型转换时有,出错的情况

lwx_dev
erdanergou 2 years ago
parent
commit
3a51a59dde
  1. 38
      src/main/java/com/dreamchaser/depository_manage/utils/ObjectFormatUtil.java

38
src/main/java/com/dreamchaser/depository_manage/utils/ObjectFormatUtil.java

@ -8,36 +8,56 @@ import java.math.BigDecimal;
public class ObjectFormatUtil { public class ObjectFormatUtil {
/** /**
* 一种较为安全的Object转换Integer方式 * 一种较为安全的Object转换Integer方式
*
* @param o 要转化的对象 * @param o 要转化的对象
* @return 转化的Integer类型结果 * @return 转化的Integer类型结果
*/ */
public static Integer toInteger(Object o){ public static Integer toInteger(Object o) {
return o==null?null:Integer.parseInt(o.toString()); if (o == null) {
return null;
}
String s = o.toString();
s = s.replaceAll(",", "");
return Integer.parseInt(s);
} }
/** /**
* 一种较为安全的Object转换Long方式 * 一种较为安全的Object转换Long方式
*
* @param o 要转化的对象 * @param o 要转化的对象
* @return 转化的Long类型结果 * @return 转化的Long类型结果
*/ */
public static Long toLong(Object o){ public static Long toLong(Object o) {
return o==null?null:Long.parseLong(o.toString()); if (o == null) {
return null;
}
String s = o.toString();
s = s.replaceAll(",", "");
return Long.parseLong(s);
} }
public static Boolean toBoolean(Object o){ public static Boolean toBoolean(Object o) {
return (o==null)?null:Boolean.parseBoolean(o.toString()); return (o == null) ? null : Boolean.parseBoolean(o.toString());
} }
public static Double toDouble(Object o){return (o == null)?null:Double.parseDouble(o.toString());} public static Double toDouble(Object o) {
if (o == null) {
return null;
}
String s = o.toString();
s = s.replaceAll(",", "");
return Double.parseDouble(s);
}
// double类型相加 // double类型相加
public static double sum(double d1,double d2){ public static double sum(double d1, double d2) {
BigDecimal bd1 = new BigDecimal(Double.toString(d1)); BigDecimal bd1 = new BigDecimal(Double.toString(d1));
BigDecimal bd2 = new BigDecimal(Double.toString(d2)); BigDecimal bd2 = new BigDecimal(Double.toString(d2));
return bd1.add(bd2).doubleValue(); return bd1.add(bd2).doubleValue();
} }
// double 类型相乘 // double 类型相乘
public static double multiply(double d1,double d2){ public static double multiply(double d1, double d2) {
BigDecimal bd1 = new BigDecimal(Double.toString(d1)); BigDecimal bd1 = new BigDecimal(Double.toString(d1));
BigDecimal bd2 = new BigDecimal(Double.toString(d2)); BigDecimal bd2 = new BigDecimal(Double.toString(d2));
return bd1.multiply(bd2).doubleValue(); return bd1.multiply(bd2).doubleValue();

Loading…
Cancel
Save