对程序中千分位符的处理

1,对List>中数据类型为BigDecimal的数据处理,新增千分位符号

public static List> getFormmatResult(){
    List> result=threadLocal.get();
    Map> fieIdDicMap=new ConcurrentHashMap<>();

    for(Map stringObjectMap: result){
        for (Map.Entry entry : stringObjectMap.entrySet()) {
            Object valObj = entry.getValue();
            String keyStr = entry.getKey();
            String typeStr = valObj.getClass().toString();
            if(StringUtils.contains(typeStr,"BigDecimal")){
                BigDecimal valBig = (BigDecimal) valObj;
                String valStr = setCellStyleAccounting(valBig.doubleValue());
                stringObjectMap.put(keyStr,valStr);
            }

        }
    }

    threadLocal.remove();
    dictValueMap.remove();
    return result;
}

小数位后面的#表示,原数据有几位小数就保留几位小数(不超过八位时)

public static String setCellStyleAccounting(double number)
{
    NumberFormat nf = new DecimalFormat(",###.########");
    String testStr = nf.format(number);
    return testStr;
}

2,单独使用格式化数据

/**
 * 新增千分位符号
 * @param number
 * @return
 */
public static String setCellStyleAccounting(double number)
{
    NumberFormat nf = new DecimalFormat(",###.########");
    String testStr = nf.format(number);
    return testStr;
}
3,字符串去除千分位符号,适用于json
/**
 * 字符串去除千分位符号,适用于json
 * @param str
 * @return
 */
public static String  replaceString(String str){
    Pattern p = Pattern.compile("\\d+,\\d+?");
    Matcher m = p.matcher(str);
    StringBuffer sb=new StringBuffer();
    while(m.find()){
        m.appendReplacement(sb,m.group().replace(",", ""));
        System.out.println(m.group());
    }
    m.appendTail(sb);
    return sb.toString();
}
 

你可能感兴趣的:(java,web)