A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 喂,咱不离! 中级黑马   /  2013-10-28 23:43  /  776 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

public class Digitizer  {
        /*
         * 12345
         * 壹万贰仟叁佰肆拾伍圆
         */
        private static char[] numArr = {'零','壹','贰','叁','肆','伍','陆','柒','捌','玖'};
        private static char[] unitArr = {'圆','拾','佰','仟','万','拾','佰','仟','亿','拾','佰','仟'};
        public static void main(String[] args) {
                System.out.println(toChinese(12345));                        //将数字转换成对应的中文
                System.out.println(toChinese(0));
                System.out.println(toChinese(10000));
                System.out.println(toChinese(1000000001));
        }
        private static String toChinese(int num) {
                StringBuilder sb = new StringBuilder();
                if(num == 0) {
                        return "零圆";
                }
                for(int i = 0; num > 0; i++) {
                        int temp = num % 10;                                        //获取num的每一个数字
                        num = num / 10;                                                        //每获取一位后就将数字去掉一位
                        sb.insert(0,unitArr[i]);                                //向0角标的位置添加单位
                        sb.insert(0, numArr[temp]);                                //向0角标的位置添加数字文字
                }
                return sb.toString()
                                .replaceAll("零[仟佰拾]", "零")
                                .replaceAll("零+", "零")
                                .replaceAll("零([亿万])", "$1")
                                .replace("亿万", "亿")
                                .replace("零圆", "圆");
        }
}


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马