金额转人民币大写是一种常见的要求,但是这一看似简单的要求,实现起来却并不容易。
前不久,我被问到了这样一道题,当时写了个算法,昨天晚上又和BJDP的人们讨论了一下,觉得很有必要把这个算法写下来。
总共36行,去掉空行还不到30行,就不多啰嗦解释了。
1
public
class
ChineseCurrencyConverter {
2
3 private static final String[] UNITS = {"亿","仟万","佰万","拾万","万","仟","佰","拾","" };
4 private static final String[] CHINESE_NUMBER = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖" };
5
6 public static String convert( long yuan) {
7
8 if (yuan >= 1e9 || yuan < 0 ) {
9 return "这个数字超出解析范围了" ;
10 }
11 String amount = String.valueOf(yuan);
12 int len = amount.length();
13 int unitsLen = UNITS.length;
14
15 String chineseCurrency = "" ;
16 for ( int i = len - 1; i >= 0; i-- ) {
17 chineseCurrency = number(amount.charAt(i)) + unit(amount.charAt(i), unitsLen - len + i) + chineseCurrency;
18 }
19
20 chineseCurrency = chineseCurrency.replaceAll("零+","零" );
21 chineseCurrency = chineseCurrency + "元整" ;
22 chineseCurrency = chineseCurrency.replaceAll("零元","元" );
23 return chineseCurrency;
24 }
25
26 private static String number( char number) {
27 return CHINESE_NUMBER[Integer.parseInt(String.valueOf(number))];
28 }
29
30 private static String unit( char number, int pos) {
31 if(number == '0' ) {
32 return "" ;
33 }
34 return UNITS[pos];
35 }
36 }
2
3 private static final String[] UNITS = {"亿","仟万","佰万","拾万","万","仟","佰","拾","" };
4 private static final String[] CHINESE_NUMBER = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖" };
5
6 public static String convert( long yuan) {
7
8 if (yuan >= 1e9 || yuan < 0 ) {
9 return "这个数字超出解析范围了" ;
10 }
11 String amount = String.valueOf(yuan);
12 int len = amount.length();
13 int unitsLen = UNITS.length;
14
15 String chineseCurrency = "" ;
16 for ( int i = len - 1; i >= 0; i-- ) {
17 chineseCurrency = number(amount.charAt(i)) + unit(amount.charAt(i), unitsLen - len + i) + chineseCurrency;
18 }
19
20 chineseCurrency = chineseCurrency.replaceAll("零+","零" );
21 chineseCurrency = chineseCurrency + "元整" ;
22 chineseCurrency = chineseCurrency.replaceAll("零元","元" );
23 return chineseCurrency;
24 }
25
26 private static String number( char number) {
27 return CHINESE_NUMBER[Integer.parseInt(String.valueOf(number))];
28 }
29
30 private static String unit( char number, int pos) {
31 if(number == '0' ) {
32 return "" ;
33 }
34 return UNITS[pos];
35 }
36 }
下面是一些用来测试的代码:
1
public
class
TestConverter {
2
3 public static void main(String[] args) {
4 System.out.println(ChineseCurrencyConverter.convert(100 ));
5 System.out.println(ChineseCurrencyConverter.convert(130 ));
6 System.out.println(ChineseCurrencyConverter.convert(102 ));
7 System.out.println(ChineseCurrencyConverter.convert(124 ));
8 System.out.println(ChineseCurrencyConverter.convert(1000 ));
9 System.out.println(ChineseCurrencyConverter.convert(10000 ));
10 System.out.println(ChineseCurrencyConverter.convert(100000 ));
11 System.out.println(ChineseCurrencyConverter.convert(1000000 ));
12 System.out.println(ChineseCurrencyConverter.convert(10000000 ));
13 System.out.println(ChineseCurrencyConverter.convert(10000000 ));
14 System.out.println(ChineseCurrencyConverter.convert(100000000 ));
15 System.out.println(ChineseCurrencyConverter.convert(1000000000 ));
16 System.out.println(ChineseCurrencyConverter.convert(1002 ));
17 System.out.println(ChineseCurrencyConverter.convert(1020 ));
18 System.out.println(ChineseCurrencyConverter.convert(1200 ));
19 System.out.println(ChineseCurrencyConverter.convert(10003 ));
20 System.out.println(ChineseCurrencyConverter.convert(10030 ));
21 System.out.println(ChineseCurrencyConverter.convert(10033 ));
22 System.out.println(ChineseCurrencyConverter.convert(10330 ));
23 System.out.println(ChineseCurrencyConverter.convert(13030 ));
24 System.out.println(ChineseCurrencyConverter.convert(100030 ));
25 System.out.println(ChineseCurrencyConverter.convert(100003 ));
26 System.out.println(ChineseCurrencyConverter.convert(100300 ));
27 System.out.println(ChineseCurrencyConverter.convert(103000 ));
28 System.out.println(ChineseCurrencyConverter.convert(100303 ));
29 System.out.println(ChineseCurrencyConverter.convert(1000004 ));
30 System.out.println(ChineseCurrencyConverter.convert(10000005 ));
31 System.out.println(ChineseCurrencyConverter.convert(10000006 ));
32 System.out.println(ChineseCurrencyConverter.convert(100000007 ));
33 System.out.println(ChineseCurrencyConverter.convert(1000000008 ));
34
35 }
36 }
2
3 public static void main(String[] args) {
4 System.out.println(ChineseCurrencyConverter.convert(100 ));
5 System.out.println(ChineseCurrencyConverter.convert(130 ));
6 System.out.println(ChineseCurrencyConverter.convert(102 ));
7 System.out.println(ChineseCurrencyConverter.convert(124 ));
8 System.out.println(ChineseCurrencyConverter.convert(1000 ));
9 System.out.println(ChineseCurrencyConverter.convert(10000 ));
10 System.out.println(ChineseCurrencyConverter.convert(100000 ));
11 System.out.println(ChineseCurrencyConverter.convert(1000000 ));
12 System.out.println(ChineseCurrencyConverter.convert(10000000 ));
13 System.out.println(ChineseCurrencyConverter.convert(10000000 ));
14 System.out.println(ChineseCurrencyConverter.convert(100000000 ));
15 System.out.println(ChineseCurrencyConverter.convert(1000000000 ));
16 System.out.println(ChineseCurrencyConverter.convert(1002 ));
17 System.out.println(ChineseCurrencyConverter.convert(1020 ));
18 System.out.println(ChineseCurrencyConverter.convert(1200 ));
19 System.out.println(ChineseCurrencyConverter.convert(10003 ));
20 System.out.println(ChineseCurrencyConverter.convert(10030 ));
21 System.out.println(ChineseCurrencyConverter.convert(10033 ));
22 System.out.println(ChineseCurrencyConverter.convert(10330 ));
23 System.out.println(ChineseCurrencyConverter.convert(13030 ));
24 System.out.println(ChineseCurrencyConverter.convert(100030 ));
25 System.out.println(ChineseCurrencyConverter.convert(100003 ));
26 System.out.println(ChineseCurrencyConverter.convert(100300 ));
27 System.out.println(ChineseCurrencyConverter.convert(103000 ));
28 System.out.println(ChineseCurrencyConverter.convert(100303 ));
29 System.out.println(ChineseCurrencyConverter.convert(1000004 ));
30 System.out.println(ChineseCurrencyConverter.convert(10000005 ));
31 System.out.println(ChineseCurrencyConverter.convert(10000006 ));
32 System.out.println(ChineseCurrencyConverter.convert(100000007 ));
33 System.out.println(ChineseCurrencyConverter.convert(1000000008 ));
34
35 }
36 }
这些数字测试的结果还都正确。
这里没有处理角和分,留给有想法的同学去尝试一下吧。
本文链接:http://www.cnblogs.com/stephen-wang/p/3155958.html,转载请注明。