输入一个long类型的整数,输出一个以金融格式表示的字符串

package test;

public class Test {
    public static void main(String[] args) { 
        System.out.println(yuan(1000000000000l));
    }
    public static String yuan(Long num){
        StringBuffer sb = new StringBuffer();
        String numStr = num.toString();
        String suffix = "."+numStr.substring(numStr.length()-2);
        String prefix = numStr.substring(0, numStr.length()-2);
        int count = 1;
        for (int i = prefix.length()-1; i >=0; i--) {
            char c = prefix.charAt(i);
            if(count%3==0)
                sb= sb.insert(0,c).insert(0, ',');
            else
                sb = sb.insert(0,c);
            count++;
        }
        sb = sb.append(suffix+"元");
        String str = sb.toString();
        return str.charAt(0)==','?str.substring(1):str;
    }
}

输出 10,000,000,000.00元

posted @ 2017-08-28 20:12  如果屈原会编程  阅读(817)  评论(0编辑  收藏  举报