第2个回答 推荐于2017-10-13
01.import java.math.BigDecimal;
02.
03.public class ConvertNumber {
04. /** 定义数组存放数字对应的大写 */
05. private final static String[] STR_NUMBER = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
06.
07. /** 定义数组存放位数的大写 */
08. private final static String[] STR_MODIFY = { "", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟" };
09.
10. /**
11. * 转化整数部分
12. *
13. * @param tempString
14. * @return 返回整数部分
15. */
16. private static String getInteger(String tempString) {
17. /** 用来保存整数部分数字串 */
18. String strInteger = null;//
19. /** 记录"."所在位置 */
20. int intDotPos = tempString.indexOf(".");
21. int intSignPos = tempString.indexOf("-");
22. if (intDotPos == -1)
23. intDotPos = tempString.length();
24. /** 取出整数部分 */
25. strInteger = tempString.substring(intSignPos + 1, intDotPos);
26. strInteger = new StringBuffer(strInteger).reverse().toString();
27. StringBuffer sbResult = new StringBuffer();
28. for (int i = 0; i < strInteger.length(); i++) {
29. sbResult.append(STR_MODIFY[i]);
30. sbResult.append(STR_NUMBER[strInteger.charAt(i) - 48]);
31. }
32.
33. sbResult = sbResult.reverse();
34. replace(sbResult, "零拾", "零");
35. replace(sbResult, "零佰", "零");
36. replace(sbResult, "零仟", "零");
37. replace(sbResult, "零万", "万");
38. replace(sbResult, "零亿", "亿");
39. replace(sbResult, "零零", "零");
40. replace(sbResult, "零零零", "零");
41. /** 这两句不能颠倒顺序 */
42. replace(sbResult, "零零零零万", "");
43. replace(sbResult, "零零零零", "");
44. /** 这样读起来更习惯. */
45. replace(sbResult, "壹拾亿", "拾亿");
46. replace(sbResult, "壹拾万", "拾万");
47. /** 删除个位上的零 */
48. if (sbResult.charAt(sbResult.length() - 1) == '零' && sbResult.length() != 1)
49. sbResult.deleteCharAt(sbResult.length() - 1);
50. if (strInteger.length() == 2) {
51. replace(sbResult, "壹拾", "拾");
52. }
53. /** 将结果反转回来. */
54. return sbResult.toString();
55. }
56.
57. /**
58. * 转化小数部分 例:输入22.34返回叁肆
59. *
60. * @param tempString
61. * @return
62. */
63. private static String getFraction(String tempString) {
64. String strFraction = null;
65. int intDotPos = tempString.indexOf(".");
66. /** 没有点说明没有小数,直接返回 */
67. if (intDotPos == -1)
68. return "";
69. strFraction = tempString.substring(intDotPos + 1);
70. StringBuffer sbResult = new StringBuffer(strFraction.length());
71. for (int i = 0; i < strFraction.length(); i++) {
72. sbResult.append(STR_NUMBER[strFraction.charAt(i) - 48]);
73. }
74. return sbResult.toString();
75. }
76.
77. /**
78. * 判断传入的字符串中是否有.如果有则返回点
79. *
80. * @param tempString
81. * @return
82. */
83. private static String getDot(String tempString) {
84. return tempString.indexOf(".") != -1 ? "点" : "";
85. }
86.
87. /**
88. * 判断传入的字符串中是否有-如果有则返回负
89. *
90. * @param tempString
91. * @return
92. */
93. private static String getSign(String tempString) {
94. return tempString.indexOf("-") != -1 ? "负" : "";
95. }
96.
97. /**
98. * 将一个数字转化为金额
99. *
100. * @param tempNumber 传入一个double的变量
101. * @return 返一个转换好的字符串
102. */
103. public static String numberToChinese(double tempNumber) {
104. java.text.DecimalFormat df = new java.text.DecimalFormat("#.#########");
105. String pTemp = String.valueOf(df.format(tempNumber));
106. StringBuffer sbResult = new StringBuffer(getSign(pTemp) + getInteger(pTemp) + getDot(pTemp) + getFraction(pTemp));
107. return sbResult.toString();
108. }
109.
110. public static String numberToChinese(BigDecimal tempNumber) {
111. return numberToChinese(tempNumber.doubleValue());
112. }
113.
114. /**
115. * 替代字符
116. *
117. * @param pValue
118. * @param pSource
119. * @param pDest
120. */
121. private static void replace(StringBuffer pValue, String pSource, String pDest) {
122. if (pValue == null || pSource == null || pDest == null)
123. return;
124. /** 记录pSource在pValue中的位置 */
125. int intPos = 0;
126. do {
127. intPos = pValue.toString().indexOf(pSource);
128. /** 没有找到pSource */
129. if (intPos == -1)
130. break;
131. pValue.delete(intPos, intPos + pSource.length());
132. pValue.insert(intPos, pDest);
133. } while (true);
134. }
135.
136. /**
137. * @param args
138. */
139. public static void main(String[] args) {
140. System.out.println(numberToChinese(1230400567.8934));
141. }
142.}本回答被提问者采纳