BigDecimal去除小数点后多余的0

产品要求页面金额展现时要去除多余的零:1.25展现1.25,1.20展现1.2,1.00 展现1。java

public static String wipeBigDecimalZero(BigDecimal number) {
		NumberFormat nf = NumberFormat.getInstance();
		return nf.format(number);
	}
	
	 public static void wipeMapBigDecimalZero(Map<String,Object> map) {
    	for(Map.Entry<String, Object> entry:map.entrySet()) {
    		if(entry.getValue() instanceof BigDecimal) {
    			map.put(entry.getKey(), NumberFormatUtil.wipeBigDecimalZero((BigDecimal)entry.getValue()));
    		}
    	}
    }
	 public static void wipeListOrMapBigDecimalZero(Object collection) {
		 if(collection instanceof Map) {
			 Map<String,Object> map = (Map<String,Object>)collection;
			 for(Map.Entry<String, Object> entry:map.entrySet()) {
				 if(entry.getValue() instanceof BigDecimal) {
					 map.put(entry.getKey(), NumberFormatUtil.wipeBigDecimalZero((BigDecimal)entry.getValue()));
				 }
			 }
		 }
		 if(collection instanceof List) {
			 List<Object> list = (List<Object>) collection;
			 for (Object object : list) {
				 if(object instanceof Map) {
					 Map<String,Object> map = (Map<String,Object>)object;
					 for(Map.Entry<String, Object> entry:map.entrySet()) {
						 if(entry.getValue() instanceof BigDecimal) {
							 map.put(entry.getKey(), NumberFormatUtil.wipeBigDecimalZero((BigDecimal)entry.getValue()));
						 }
					 }
				 }
			}
		 }
	 }
相关文章
相关标签/搜索