1 public class NumberToCNUtil {
2
3 private NumberToCNUtil() { throw new IllegalStateException("Utility class"); }
4
5 /**
6 * 汉语中数字大写
7 */
8 private static final String[] CN_UPPER_NUMBER = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
9 /**
10 * 汉语中货币单位大写,这样的设计类似于占位符
11 */
12 private static final String[] CN_UPPER_MONETRAY_UNIT = { "分", "角", "元",
13 "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾",
14 "佰", "仟" };
15 /**
16 * 特殊字符:整
17 */
18 private static final String CN_FULL = "整";
19 /**
20 * 特殊字符:负
21 */
22 private static final String CN_NEGATIVE = "负";
23 /**
24 * 金额的精度,默认值为2
25 */
26 private static final int MONEY_PRECISION = 2;
27 /**
28 * 特殊字符:零元整
29 */
30 private static final String CN_ZEOR_FULL = "零元" + CN_FULL;
31
32 /**
33 * 把输入的金额转换为汉语中人民币的大写
34 * @param numberOfMoney 输入的金额
35 * @return 对应的汉语大写
36 */
37 public static String number2CNMontrayUnit(BigDecimal numberOfMoney) {
38 StringBuilder sb = new StringBuilder();
39 // -1, 0, or 1 as the value of this BigDecimal is negative, zero, or
40 // positive.
41 int signum = numberOfMoney.signum();
42 // 零元整的情况
43 if (signum == 0) {
44 return CN_ZEOR_FULL;
45 }
46 //这里会进行金额的四舍五入
47 long number = numberOfMoney.movePointRight(MONEY_PRECISION)
48 .setScale(0, 4).abs().longValue();
49 // 得到小数点后两位值
50 long scale = number % 100;
51 int numUnit = 0;
52 int numIndex = 0;
53 boolean getZero = false;
54 // 判断最后两位数,一共有四中情况:00 = 0, 01 = 1, 10, 11
55 if (scale <= 0) {
56 numIndex = 2;
57 number = number / 100;
58 getZero = true;
59 }
60 if ((scale > 0) && (scale % 10 <= 0)) {
61 numIndex = 1;
62 number = number / 10;
63 getZero = true;
64 }
65 int zeroSize = 0;
66 while (true) {
67 if (number <= 0) {
68 break;
69 }
70 // 每次获取到最后一个数
71 numUnit = (int) (number % 10);
72 if (numUnit > 0) {
73 if ((numIndex == 9) && (zeroSize >= 3)) {
74 sb.insert(0, CN_UPPER_MONETRAY_UNIT[6]);
75 }
76 if ((numIndex == 13) && (zeroSize >= 3)) {
77 sb.insert(0, CN_UPPER_MONETRAY_UNIT[10]);
78 }
79 sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
80 sb.insert(0, CN_UPPER_NUMBER[numUnit]);
81 getZero = false;
82 zeroSize = 0;
83 } else {
84 ++zeroSize;
85 if (!(getZero)) {
86 sb.insert(0, CN_UPPER_NUMBER[numUnit]);
87 }
88 if (numIndex == 2) {
89 if (number > 0) {
90 sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
91 }
92 } else if (((numIndex - 2) % 4 == 0) && (number % 1000 > 0)) {
93 sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
94 }
95 getZero = true;
96 }
97 // 让number每次都去掉最后一个数
98 number = number / 10;
99 ++numIndex;
100 }
101 // 如果signum == -1,则说明输入的数字为负数,就在最前面追加特殊字符:负
102 if (signum == -1) {
103 sb.insert(0, CN_NEGATIVE);
104 }
105 // 输入的数字小数点后两位为"00"的情况,则要在最后追加特殊字符:整
106 if (scale <= 0) {
107 sb.append(CN_FULL);
108 }
109 return sb.toString();
110 }
111
112 public static String number2CNMontrayUnit(String numberOfMoney){
113 BigDecimal money = new BigDecimal(numberOfMoney);
114 return number2CNMontrayUnit(money);
115 }
116 public static String number2CNMontrayUnit(Double numberOfMoney){
117 BigDecimal money = new BigDecimal(String.valueOf(numberOfMoney));
118 return number2CNMontrayUnit(money);
119 }
120 }