Java 实现金额转换 代码示例
金额转换,阿拉伯数字的金额转换成中国传统的形式如:
(¥1011)→(壹仟零壹拾壹元整)输出。
分析:
金额转换,在开发财务相关软件时会经常用到,也是软件本地化的一个需要。一般开发公司或者团队都有相应的金额转换类或者是模块,配合报表工具,可以实现良好的本地化。这里给出一个简单的金额转换代码,供读者参考。
1. package programe;
2. import java.text.NumberFormat;
3. import java.util.HashMap;
4. import java.util.Scanner;
5. public class AmountOfConversion {
6. //定义HashMap的value值
7. public static final String EMPTY = "";
8. public static final String ZERO = "零";
9. public static final String ONE = "壹";
10. public static final String TWO = "贰";
11. public static final String THREE = "叁";
12. public static final String FOUR = "肆";
13. public static final String FIVE = "伍";
14. public static final String SIX = "陆";
15. public static final String SEVEN = "柒";
16. public static final String EIGHT = "捌";
17. public static final String NINE = "玖";
18. public static final String TEN = "拾";
19. public static final String HUNDRED = "佰";
20. public static final String THOUSAND = "仟";
21. public static final String TEN_THOUSAND = "万";
22. public static final String HUNDRED_MILLION = "亿";
23. public static final String YUAN = "元";
24. public static final String JIAO = "角";
25. public static final String FEN = "分";
26. public static final String DOT = ".";
27. private static AmountOfConversion formatter = null;
//创建AmountOfConversion的实例对象
28. //创建初始化一个HashMap对象
29. private HashMap NumberMap = new HashMap();
30. private HashMap HighnumberofMoney = new HashMap();
31. //创建初始化一个NumberFormat对象
32. private NumberFormat numberFormat =
NumberFormat.getInstance();
33. private AmountOfConversion() {
34. //在用new创建AmountOfConversion对象时,
为HashMap对象进行key-value的映射
35. numberFormat.setMaximumFractionDigits(4);
//设置数据的小数部分的最大位数是4位
36. numberFormat.setMinimumFractionDigits(2);
//设置数据的小数部分的最小位数是2位
37. numberFormat.setGroupingUsed(false);
/*设置此格式中是不可以使用组。如果设置可以使用组,
则数 1234567 可能被格式化为 "1,234,567"*/
38. NumberMap.put("0", ZERO);
39. NumberMap.put("1", ONE);
40. NumberMap.put("2", TWO);
41. NumberMap.put("3", THREE);
42. NumberMap.put("4", FOUR);
43. NumberMap.put("5", FIVE);
44. NumberMap.put("6", SIX);
45. NumberMap.put("7", SEVEN);
46. NumberMap.put("8", EIGHT);
47. NumberMap.put("9", NINE);
48. NumberMap.put(DOT, DOT);
49. HighnumberofMoney.put("1", TEN);
50. HighnumberofMoney.put("2", HUNDRED);
51. HighnumberofMoney.put("3", THOUSAND);
52. HighnumberofMoney.put("4", TEN_THOUSAND);
53. HighnumberofMoney.put("5", TEN);
54. HighnumberofMoney.put("6", HUNDRED);
55. HighnumberofMoney.put("7", THOUSAND);
56. HighnumberofMoney.put("8", HUNDRED_MILLION);
57. }
58. public static AmountOfConversion getInstance() {
//判断AmountOfConversion对象formatter是否初始化
59. if (formatter == null)
60. formatter = new AmountOfConversion();
61. return formatter;
62. }
63. //进行金额转换的多种数据类型
64. public String format(String moneyStr) {
65. String result = "不能进行金额转换!!";
66. if(isConversion(moneyStr)){
67. result = convertIntegerTochinese(moneyStr);
68. result = convertPointTochinese(result);
69. }
70. return result;
71. }
72. public String format(double moneyDouble) {
73. return format(numberFormat.format(moneyDouble));
74. }
75. public String format(int moneyInt) {
76. return format(numberFormat.format(moneyInt));
77. }
78. public String format(long moneyLong) {
79. return format(numberFormat.format(moneyLong));
80. }
81. public String format(Number moneyNum) {
82. return format(numberFormat.format(moneyNum));
83. }
84. private String convertIntegerTochinese(String moneyStr) {
//将参数中传入的阿拉伯数字转换成中文
85. String result;
86. StringBuffer C2CStrBufer = new StringBuffer();
87. for (int i = 0; i < moneyStr.length(); i++) {
88. C2CStrBufer.append(NumberMap.get(moneyStr.substring(
89. i, i + 1)));
90. }
91. // 拾佰仟万亿等都是汉字里面才有的单位,加上它们
92. int indexOfDot = C2CStrBufer.indexOf(DOT);
93. int moneyPatternCursor = 1;
94. for (int i = indexOfDot - 1; i > 0; i--) {
95. C2CStrBufer.insert(i, HighnumberofMoney.get(EMPTY
96. + moneyPatternCursor));
97. moneyPatternCursor = moneyPatternCursor == 8 ? 1
98. : moneyPatternCursor + 1;
99. }
100. String fractionPart = C2CStrBufer.substring(C2CStrBufer
101. .indexOf("."));
102. C2CStrBufer.delete(C2CStrBufer.indexOf("."),
103. C2CStrBufer.length());
104. while (C2CStrBufer.indexOf("零拾") != -1) {
105. C2CStrBufer.replace(C2CStrBufer.indexOf("零拾"),
106. C2CStrBufer.indexOf("零拾") + 2, ZERO);
107. }
108. while (C2CStrBufer.indexOf("零佰") != -1) {
109. C2CStrBufer.replace(C2CStrBufer.indexOf("零佰"),
110. C2CStrBufer.indexOf("零佰") + 2, ZERO);
111. }
112. while (C2CStrBufer.indexOf("零仟") != -1) {
113. C2CStrBufer.replace(C2CStrBufer.indexOf("零仟"),
114. C2CStrBufer.indexOf("零仟") + 2, ZERO);
115. }
116. while (C2CStrBufer.indexOf("零万") != -1) {
117. C2CStrBufer.replace(C2CStrBufer.indexOf("零万"),
118. C2CStrBufer.indexOf("零万") + 2, TEN_THOUSAND);
119. }
120. while (C2CStrBufer.indexOf("零亿") != -1) {
121. C2CStrBufer.replace(C2CStrBufer.indexOf("零亿"),
122. C2CStrBufer.indexOf("零亿") + 2, HUNDRED_MILLION);
123. }
124. while (C2CStrBufer.indexOf("零零") != -1) {
125. C2CStrBufer.replace(C2CStrBufer.indexOf("零零"),
126. C2CStrBufer.indexOf("零零") + 2, ZERO);
127. }
128. if (C2CStrBufer.lastIndexOf(ZERO) == C2CStrBufer.length() - 1)
129. C2CStrBufer.delete(C2CStrBufer.length() - 1,
130. C2CStrBufer.length());
131. C2CStrBufer.append(fractionPart);
132. result = C2CStrBufer.toString();
133. return result;
134. }
135. private String convertPointTochinese(String moneyStr) {
//对小数点后面的数字进行汉字处理
136. String result;
137. StringBuffer C2CStrBufer = new StringBuffer(moneyStr);
138. int indexOfDot = C2CStrBufer.indexOf(DOT);
139. C2CStrBufer.replace(indexOfDot, indexOfDot + 1, YUAN);
140. C2CStrBufer.insert(C2CStrBufer.length() - 1, JIAO);
141. C2CStrBufer.insert(C2CStrBufer.length(), FEN);
142. if (C2CStrBufer.indexOf("零角零分") != -1)// 没有零头,加整
143. C2CStrBufer.replace(C2CStrBufer.indexOf("零角零分"),
144. C2CStrBufer.length(), "整");
145. else if (C2CStrBufer.indexOf("零分") != -1)// 没有零分,加整
146. C2CStrBufer.replace(C2CStrBufer.indexOf("零分"),
147. C2CStrBufer.length(), "整");
148. else {
149. if (C2CStrBufer.indexOf("零角") != -1)
150. C2CStrBufer.delete(C2CStrBufer.indexOf("零角"),
151. C2CStrBufer.indexOf("零角") + 2);
152. }
153. result = C2CStrBufer.toString();
154. return result;
155. }
156. private boolean isConversion(String moneyStr) {
//判断参数传来的数据是否符合进行转换的条件
157. int fractionDigits = moneyStr.length() -
moneyStr.indexOf(DOT) - 1;
158. boolean flag = true;
159. if (fractionDigits > 2){
160. System.out.println("金额" + moneyStr + "
的小数位多于两位。");
// 精度不能比分低
161. flag = false;
162. }
163. return flag;
164.
165. }
166. public static void main(String args[]) {
167. System.out.println("请输入金额数:");
168. Scanner scanner = new Scanner(System.in);
169. String str = scanner.next();
170. System.out.println("转换后的金额为:");
171. System.out.println(getInstance().format(new Double(str)));
172. }
}