如何给数字添加分隔符

   public static String formatNumber( String iniNum, int split ){
        // ① 去掉所有逗号,并把串倒过来。
        StringBuffer tmp = new StringBuffer().append( iniNum.replaceAll(",", "") ) .reverse();


        // ② 替换这样的串:连续split位数字的串,其右边还有个数字,在串的右边添加逗号
        String retNum = Pattern.compile( "(\\d{" + split + "})(?=\\d)" ) .matcher( tmp.toString() ).replaceAll("$1,");


        // ③ 替换完后,再把串倒回去返回
        return new StringBuffer().append( retNum ).reverse().toString();
    }

posted @ 2017-05-11 14:08  浪里小白龙呼呼呼  阅读(1656)  评论(0编辑  收藏  举报