金额千分位分割

public static void main(String[] args) {
        BigDecimal b = new BigDecimal("10000000000000000.12312");
        System.out.println(decimalFormat(b));
    }
    
    private static String decimalFormat(BigDecimal amount) {
        if(amount == null) {
            return "";
        }
        amount = amount.divide(BigDecimal.ONE,2, BigDecimal.ROUND_HALF_UP);
        String[] s = amount.toString().split("\\.");
        StringBuffer buffer = new StringBuffer();
        char[] cs = s[0].toCharArray();
        for(int i = cs.length - 1;i >= 0;i --) {
            buffer.append(cs[cs.length-i-1]);
            if((i)%3==0 && cs.length > 3 && i != 0) {
                buffer.append(",");
            }
        }
        buffer.append(".").append(s[1]);
        return buffer.toString();
    }
posted @ 2020-09-02 18:30  我的博客我的人生  阅读(441)  评论(0编辑  收藏  举报