30行,金额转人民币大写的代码

金额转人民币大写是一种常见的要求,但是这一看似简单的要求,实现起来却并不容易。

前不久,我被问到了这样一道题,当时写了个算法,昨天晚上又和BJDP的人们讨论了一下,觉得很有必要把这个算法写下来。

总共37行,去掉空行还不到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         chineseCurrency = chineseCurrency.replaceAll("零元","元");

24         return chineseCurrency;

25     }

26 

27     private static String number(char number) {

28         return CHINESE_NUMBER[Integer.parseInt(String.valueOf(number))];

29     }

30 

31     private static String unit(char number, int pos) {

32         if(number == '0' && pos != UNITS.length - 5) {

33             return "";

34         }

35         return UNITS[pos];

36     }

37 }

 

 

下面是一些用来测试的代码:

 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(12000006));

33         System.out.println(ChineseCurrencyConverter.convert(10030006));

34         System.out.println(ChineseCurrencyConverter.convert(10200006));

35         System.out.println(ChineseCurrencyConverter.convert(100000007));

36         System.out.println(ChineseCurrencyConverter.convert(1000000008));

37 

38     }

39 }

 

这些数字测试的结果还都正确。

这里没有处理角和分,留给有想法的同学去尝试一下吧。

 

你可能感兴趣的:(代码)