java常用的object数据处理

// 将Object转换为Double 

public static Double changeTypeD(Object obj) {
try {
if(obj!=null){
return new BigDecimal(Double.valueOf(obj + "")).setScale(2,
BigDecimal.ROUND_HALF_UP).doubleValue();
}else{
return 0.0;
}
} catch (Exception e) {
return 0.0;
}
}
// 将Object转换为Integer
public static Integer changeTypeI(Object obj) {
try {
if(obj!=null){
return StringUtils.toInteger(obj);
}else{
return 0;
}
} catch (Exception e) {
return 0;
}
}

// 将String转换为Integer
public static String changeTypeS(Object obj) {
try {
return StringUtils.toInteger(obj).toString();
} catch (Exception e) {
return "";
}
}

/**
* 将Map中数字为0的设置为null
*
* @param map
* @author: NQY
* @Createtime: 2017年3月24日 上午9:22:09
*/
public static void setValueIsNull(Map map) {
if (map != null && map.size() > 0) {
Iterator it = map.keySet().iterator();
String key = null;

while (it.hasNext()) {
key = it.next().toString();

Object obj = map.get(key);
if (obj != null) {
if (NumberUtils.isNumber(obj.toString())) {
double val = Double.parseDouble(obj.toString());

if (val == 0.0) {
map.put(key, null);
}else if(val%1==0){
map.put(key, (int)Math.ceil(val));
}else if(val%1!=0){
DecimalFormat df = new DecimalFormat("#.00");
map.put(key, df.format(val));
}
}
}
}
}
}
/**
* 处理人数(个位数)除以10000
* @param obj
*/
public static String dealDouble(Object obj){
DecimalFormat df = new DecimalFormat(DealReportUtil.changeTypeD(obj)<10000?"0.0000":"#.0000");
return df.format(DealReportUtil.changeTypeD(obj)/10000);
}

原文地址:https://www.cnblogs.com/shuzhenzhumo/p/9298078.html