【算法】将阿拉伯数字转为中文大写

100689123
转换为:
壹亿零佰陆拾捌万玖仟壹佰贰拾叁元

直接上代码:
 1 package main;
 2 
 3 import java.util.concurrent.ExecutorService;
 4 import java.util.concurrent.Executors;
 5 import java.util.concurrent.locks.Condition;
 6 import java.util.concurrent.locks.Lock;
 7 import java.util.concurrent.locks.ReentrantLock;
 8 
 9 public class Main {
10     public static void main(String[] args) {
11         System.out.println(cov(convert(100689123)));
12     }
13 
14     private static final char[] data = new char[]{'零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'};
15     private static final char[] units = new char[]{'元', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿'};
16 
17     /**
18      * 对替换掉的数字进行处理
19      * @param str
20      * @return
21      */
22     private static String cov(String str) {
23         StringBuffer sb = new StringBuffer(str);
24 
25         //System.out.println("1"+sb.reverse().toString());
26         str = sb.reverse().toString().replaceAll("零[拾佰仟]", "零").replaceAll("零+万", "万").replaceAll("零+元", "元").replaceAll("零+", "零");
27         System.out.println("2"+str);
28         return new StringBuffer(str).reverse().toString();
29     }
30 
31     /**
32      * 现将各个后面统统加上单位
33      * @param money
34      * @return
35      */
36     public static String convert(int money) {
37         StringBuffer sbf = new StringBuffer();
38         int unit = 0;
39         while (money != 0) {
40             sbf.insert(0, units[unit++]);
41             int number = money % 10;
42             sbf.insert(0, data[number]);
43             money /= 10;
44         }
45         System.out.println(sbf.toString());
46         return sbf.toString();
47     }
48 }

 

posted @ 2018-08-16 15:06  程小白  阅读(1720)  评论(0编辑  收藏  举报