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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© lovecx24 中级黑马   /  2013-11-29 18:07  /  2007 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

阿拉伯数字转化成人民币大写程序大家也见过很多,但是这个程序在我看来是功能最强大的,该程序考虑到小数问题,输入不规范问题,数值大小问题等等各种问题,该程序十分健壮!
  1. import java.text.NumberFormat;
  2. import java.util.HashMap;
  3. public class ConvertChinesQ {
  4. public static final String EMPTY = "";
  5. public static final String ZERO = "零";
  6. public static final String ONE = "壹";
  7. public static final String TWO = "贰";
  8. public static final String THREE = "叁";
  9. public static final String FOUR = "肆";
  10. public static final String FIVE = "伍";
  11. public static final String SIX = "陆";
  12. public static final String SEVEN = "柒";
  13. public static final String EIGHT = "捌";
  14. public static final String NINE = "玖";
  15. public static final String TEN = "拾";
  16. public static final String HUNDRED = "佰";
  17. public static final String THOUSAND = "仟";
  18. public static final String TEN_THOUSAND = "万";
  19. public static final String HUNDRED_MILLION = "亿";
  20. public static final String YUAN = "元";
  21. public static final String JIAO = "角";
  22. public static final String FEN = "分";
  23. public static final String DOT = ".";
  24. private static ConvertChinesQ formatter = null;
  25. private HashMap chineseNumberMap = new HashMap();
  26. private HashMap chineseMoneyPattern = new HashMap();
  27. private NumberFormat numberFormat = NumberFormat.getInstance();
  28. private ConvertChinesQ() {
  29.   numberFormat.setMaximumFractionDigits(4);
  30.   numberFormat.setMinimumFractionDigits(2);
  31.   numberFormat.setGroupingUsed(false);
  32.   chineseNumberMap.put("0", ZERO);
  33.   chineseNumberMap.put("1", ONE);
  34.   chineseNumberMap.put("2", TWO);
  35.   chineseNumberMap.put("3", THREE);
  36.   chineseNumberMap.put("4", FOUR);
  37.   chineseNumberMap.put("5", FIVE);
  38.   chineseNumberMap.put("6", SIX);
  39.   chineseNumberMap.put("7", SEVEN);
  40.   chineseNumberMap.put("8", EIGHT);
  41.   chineseNumberMap.put("9", NINE);
  42.   chineseNumberMap.put(DOT, DOT);
  43.   chineseMoneyPattern.put("1", TEN);
  44.   chineseMoneyPattern.put("2", HUNDRED);
  45.   chineseMoneyPattern.put("3", THOUSAND);
  46.   chineseMoneyPattern.put("4", TEN_THOUSAND);
  47.   chineseMoneyPattern.put("5", TEN);
  48.   chineseMoneyPattern.put("6", HUNDRED);
  49.   chineseMoneyPattern.put("7", THOUSAND);
  50.   chineseMoneyPattern.put("8", HUNDRED_MILLION);
  51. }
  52. public static ConvertChinesQ getInstance() {
  53.   if (formatter == null)
  54.    formatter = new ConvertChinesQ();
  55.   return formatter;
  56. }
  57. public String format(String moneyStr) {
  58.   checkPrecision(moneyStr);
  59.   String result;
  60.   result = convertToChineseNumber(moneyStr);
  61.   result = addUnitsToChineseMoneyString(result);
  62.   return result;
  63. }
  64. public String format(double moneyDouble) {
  65.   return format(numberFormat.format(moneyDouble));
  66. }
  67. public String format(int moneyInt) {
  68.   return format(numberFormat.format(moneyInt));
  69. }
  70. public String format(long moneyLong) {
  71.   return format(numberFormat.format(moneyLong));
  72. }
  73. public String format(Number moneyNum) {
  74.   return format(numberFormat.format(moneyNum));
  75. }
  76. private String convertToChineseNumber(String moneyStr) {
  77.   String result;
  78.   StringBuffer cMoneyStringBuffer = new StringBuffer();
  79.   for (int i = 0; i < moneyStr.length(); i++) {
  80.    cMoneyStringBuffer.append(chineseNumberMap.get(moneyStr.substring(
  81.      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
  88.      + moneyPatternCursor));
  89.    moneyPatternCursor = moneyPatternCursor == 8 ? 1
  90.      : moneyPatternCursor + 1;
  91.   }
  92.   String fractionPart = cMoneyStringBuffer.substring(cMoneyStringBuffer
  93.     .indexOf("."));
  94.   cMoneyStringBuffer.delete(cMoneyStringBuffer.indexOf("."),
  95.     cMoneyStringBuffer.length());
  96.   while (cMoneyStringBuffer.indexOf("零拾") != -1) {
  97.    cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零拾"),
  98.      cMoneyStringBuffer.indexOf("零拾") + 2, ZERO);
  99.   }
  100.   while (cMoneyStringBuffer.indexOf("零佰") != -1) {
  101.    cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零佰"),
  102.      cMoneyStringBuffer.indexOf("零佰") + 2, ZERO);
  103.   }
  104.   while (cMoneyStringBuffer.indexOf("零仟") != -1) {
  105.    cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零仟"),
  106.      cMoneyStringBuffer.indexOf("零仟") + 2, ZERO);
  107.   }
  108.   while (cMoneyStringBuffer.indexOf("零万") != -1) {
  109.    cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零万"),
  110.      cMoneyStringBuffer.indexOf("零万") + 2, TEN_THOUSAND);
  111.   }
  112.   while (cMoneyStringBuffer.indexOf("零亿") != -1) {
  113.    cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零亿"),
  114.      cMoneyStringBuffer.indexOf("零亿") + 2, HUNDRED_MILLION);
  115.   }
  116.   while (cMoneyStringBuffer.indexOf("零零") != -1) {
  117.    cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零零"),
  118.      cMoneyStringBuffer.indexOf("零零") + 2, ZERO);
  119.   }
  120.   if (cMoneyStringBuffer.lastIndexOf(ZERO) == cMoneyStringBuffer.length() - 1)
  121.    cMoneyStringBuffer.delete(cMoneyStringBuffer.length() - 1,
  122.      cMoneyStringBuffer.length());
  123.   cMoneyStringBuffer.append(fractionPart);
  124.   result = cMoneyStringBuffer.toString();
  125.   return result;
  126. }
  127. private String addUnitsToChineseMoneyString(String moneyStr) {
  128.   String result;
  129.   StringBuffer cMoneyStringBuffer = new StringBuffer(moneyStr);
  130.   int indexOfDot = cMoneyStringBuffer.indexOf(DOT);
  131.   cMoneyStringBuffer.replace(indexOfDot, indexOfDot + 1, YUAN);
  132.   cMoneyStringBuffer.insert(cMoneyStringBuffer.length() - 1, JIAO);
  133.   cMoneyStringBuffer.insert(cMoneyStringBuffer.length(), FEN);
  134.   if (cMoneyStringBuffer.indexOf("零角零分") != -1)//没有零头,加整
  135.    cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零角零分"),
  136.      cMoneyStringBuffer.length(), "整");
  137.   else if (cMoneyStringBuffer.indexOf("零分") != -1)//没有零分,加整
  138.    cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零分"),
  139.      cMoneyStringBuffer.length(), "整");
  140.   else {
  141.    if (cMoneyStringBuffer.indexOf("零角") != -1)
  142.     cMoneyStringBuffer.delete(cMoneyStringBuffer.indexOf("零角"),
  143.       cMoneyStringBuffer.indexOf("零角") + 2);
  144.    // tmpBuffer.append("整");
  145.   }
  146.   result = cMoneyStringBuffer.toString();
  147.   return result;
  148. }
  149. private void checkPrecision(String moneyStr) {
  150.   int fractionDigits = moneyStr.length() - moneyStr.indexOf(DOT) - 1;
  151.   if (fractionDigits > 2)
  152.    throw new RuntimeException("金额" + moneyStr + "的小数位多于两位。"); //精度不能比分低
  153. }
  154. public static void main(String args[]) throws Exception{
  155.   System.out.println(getInstance().format(new Double(10000001.01)));
  156.   throw new Exception("slkdfjsk");
  157. }
  158. }
复制代码

1 个回复

倒序浏览
顶一个!!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马