A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 张向辉 于 2013-2-2 12:44 编辑

金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)->(一千零一拾一元整)输出。
  1. package test.format;
  2. import java.text.NumberFormat;
  3. import java.util.HashMap;
  4. public class SimpleMoneyFormat {
  5. public static final String EMPTY = "";
  6. public static final String ZERO = "零";
  7. public static final String ONE = "壹";
  8. public static final String TWO = "贰";
  9. public static final String THREE = "叁";
  10. public static final String FOUR = "肆";
  11. public static final String FIVE = "伍";
  12. public static final String SIX = "陆";
  13. public static final String SEVEN = "柒";
  14. public static final String EIGHT = "捌";
  15. public static final String NINE = "玖";
  16. public static final String TEN = "拾";
  17. public static final String HUNDRED = "佰";
  18. public static final String THOUSAND = "仟";
  19. public static final String TEN_THOUSAND = "万";
  20. public static final String HUNDRED_MILLION = "亿";
  21. public static final String YUAN = "元";
  22. public static final String JIAO = "角";
  23. public static final String FEN = "分";
  24. public static final String DOT = ".";

  25. private static SimpleMoneyFormat formatter = null;
  26. private HashMap chineseNumberMap = new HashMap();
  27. private HashMap chineseMoneyPattern = new HashMap();
  28. private NumberFormat numberFormat = NumberFormat.getInstance();

  29. private SimpleMoneyFormat() {
  30. numberFormat.setMaximumFractionDigits(4);
  31. numberFormat.setMinimumFractionDigits(2);
  32. numberFormat.setGroupingUsed(false);

  33. chineseNumberMap.put("0", ZERO);
  34. chineseNumberMap.put("1", ONE);
  35. chineseNumberMap.put("2", TWO);
  36. chineseNumberMap.put("3", THREE);
  37. chineseNumberMap.put("4", FOUR);
  38. chineseNumberMap.put("5", FIVE);
  39. chineseNumberMap.put("6", SIX);
  40. chineseNumberMap.put("7", SEVEN);
  41. chineseNumberMap.put("8", EIGHT);
  42. chineseNumberMap.put("9", NINE);
  43. chineseNumberMap.put(DOT, DOT);

  44. chineseMoneyPattern.put("1", TEN);
  45. chineseMoneyPattern.put("2", HUNDRED);
  46. chineseMoneyPattern.put("3", THOUSAND);
  47. chineseMoneyPattern.put("4", TEN_THOUSAND);
  48. chineseMoneyPattern.put("5", TEN);
  49. chineseMoneyPattern.put("6", HUNDRED);
  50. chineseMoneyPattern.put("7", THOUSAND);
  51. chineseMoneyPattern.put("8", HUNDRED_MILLION);
  52. }

  53. public static SimpleMoneyFormat getInstance() {
  54. if (formatter == null)
  55. formatter = new SimpleMoneyFormat();
  56. return formatter;
  57. }

  58. public String format(String moneyStr) {
  59. checkPrecision(moneyStr);
  60. String result;
  61. result = convertToChineseNumber(moneyStr);
  62. result = addUnitsToChineseMoneyString(result);
  63. return result;
  64. }

  65. public String format(double moneyDouble) {
  66. return format(numberFormat.format(moneyDouble));
  67. }

  68. public String format(int moneyInt) {
  69. return format(numberFormat.format(moneyInt));
  70. }

  71. public String format(long moneyLong) {
  72. return format(numberFormat.format(moneyLong));
  73. }

  74. public String format(Number moneyNum) {
  75. return format(numberFormat.format(moneyNum));
  76. }

  77. private String convertToChineseNumber(String moneyStr) {
  78. String result;
  79. StringBuffer cMoneyStringBuffer = new StringBuffer();
  80. for (int i = 0; i < moneyStr.length(); i++) {
  81. cMoneyStringBuffer.append(chineseNumberMap.get(moneyStr.substring(i, i + 1)));
  82. }
  83. //拾佰仟万亿等都是汉字里面才有的单位,加上它们
  84. int indexOfDot = cMoneyStringBuffer.indexOf(DOT);
  85. int moneyPatternCursor = 1;
  86. for (int i = indexOfDot - 1; i > 0; i--) {
  87. cMoneyStringBuffer.insert(i, chineseMoneyPattern.get(EMPTY + moneyPatternCursor));
  88. moneyPatternCursor = moneyPatternCursor == 8 ? 1 : moneyPatternCursor + 1;
  89. }

  90. String fractionPart = cMoneyStringBuffer.substring(cMoneyStringBuffer.indexOf("."));
  91. cMoneyStringBuffer.delete(cMoneyStringBuffer.indexOf("."), cMoneyStringBuffer.length());
  92. while (cMoneyStringBuffer.indexOf("零拾") != -1) {
  93. cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零拾"), cMoneyStringBuffer.indexOf("零拾") + 2, ZERO);
  94. }
  95. while (cMoneyStringBuffer.indexOf("零佰") != -1) {
  96. cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零佰"), cMoneyStringBuffer.indexOf("零佰") + 2, ZERO);
  97. }
  98. while (cMoneyStringBuffer.indexOf("零仟") != -1) {
  99. cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零仟"), cMoneyStringBuffer.indexOf("零仟") + 2, ZERO);
  100. }
  101. while (cMoneyStringBuffer.indexOf("零万") != -1) {
  102. cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零万"), cMoneyStringBuffer.indexOf("零万") + 2, TEN_THOUSAND);
  103. }
  104. while (cMoneyStringBuffer.indexOf("零亿") != -1) {
  105. cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零亿"), cMoneyStringBuffer.indexOf("零亿") + 2, HUNDRED_MILLION);
  106. }
  107. while (cMoneyStringBuffer.indexOf("零零") != -1) {
  108. cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零零"), cMoneyStringBuffer.indexOf("零零") + 2, ZERO);
  109. }
  110. if (cMoneyStringBuffer.lastIndexOf(ZERO) == cMoneyStringBuffer.length() - 1)
  111. cMoneyStringBuffer.delete(cMoneyStringBuffer.length() - 1, cMoneyStringBuffer.length());
  112. cMoneyStringBuffer.append(fractionPart);

  113. result = cMoneyStringBuffer.toString();
  114. return result;
  115. }


  116. private String addUnitsToChineseMoneyString(String moneyStr) {
  117. String result;
  118. StringBuffer cMoneyStringBuffer = new StringBuffer(moneyStr);
  119. int indexOfDot = cMoneyStringBuffer.indexOf(DOT);
  120. cMoneyStringBuffer.replace(indexOfDot, indexOfDot + 1, YUAN);
  121. cMoneyStringBuffer.insert(cMoneyStringBuffer.length() - 1, JIAO);
  122. cMoneyStringBuffer.insert(cMoneyStringBuffer.length(), FEN);
  123. if (cMoneyStringBuffer.indexOf("零角零分") != -1)//没有零头,加整
  124. cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零角零分"), cMoneyStringBuffer.length(), "整");
  125. else
  126. if (cMoneyStringBuffer.indexOf("零分") != -1)//没有零分,加整
  127. cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零分"), cMoneyStringBuffer.length(), "整");
  128. else {
  129. if(cMoneyStringBuffer.indexOf("零角")!=-1)
  130. cMoneyStringBuffer.delete(cMoneyStringBuffer.indexOf("零角"),cMoneyStringBuffer.indexOf("零角")+2);
  131. // tmpBuffer.append("整");
  132. }
  133. result = cMoneyStringBuffer.toString();
  134. return result;
  135. }

  136. private void checkPrecision(String moneyStr) {
  137. int fractionDigits = moneyStr.length() - moneyStr.indexOf(DOT) - 1;
  138. if (fractionDigits > 2)
  139. throw new RuntimeException("金额" + moneyStr + "的小数位多于两位。"); //精度不能比分低
  140. }

  141. public static void main(String args[]) {
  142. System.out.println(getInstance().format(new Double(10010001.01)));
  143. }
  144. }

复制代码

评分

参与人数 1黑马币 +40 收起 理由
杨志 + 40

查看全部评分

4 个回复

倒序浏览
应该可以改一下,能否考虑从数组或者集合入手。占个沙发,继续关注!
回复 使用道具 举报
静待{:soso_e127:}优化
回复 使用道具 举报
建议去看看代码重构,代码之美两本书。
这个真的很经典的。
回复 使用道具 举报
二进制和ASCII码之间线程30H,可以在二进制之间加30h,就变成ASCII码,在计算机中显示了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马