java实现小写金额转换大写金额

  1. package com.sunboon.jiexi;  
  2.   
  3. /** 
  4.  *  
  5.  * @author qizhenglong 
  6.  *  
  7.  */  
  8. public class MoneyUtil {  
  9.     /** 大写数字 */  
  10.     private static final String[] NUMBERS = { "零""壹""贰""叁""肆""伍",  
  11.             "陆""柒""捌""玖" };  
  12.   
  13.     /** 整数部分的单位 */  
  14.     private static final String[] IUNIT = { "元""拾""佰""仟""万""拾""佰",  
  15.             "仟""亿""拾""佰""仟""万""拾""佰""仟" };  
  16.   
  17.     /** 小数部分的单位 */  
  18.     private static final String[] DUNIT = { "角""分""厘" };  
  19.   
  20.     /** 
  21.      * 得到大写金额。 
  22.      */  
  23.     public static String toChinese(String str) {  
  24.         str = str.replaceAll(",""");// 去掉","  
  25.         String integerStr;// 整数部分数字  
  26.         String decimalStr;// 小数部分数字  
  27.         // 初始化:分离整数部分和小数部分  
  28.         if (str.indexOf(".") > 0) {  
  29.             integerStr = str.substring(0, str.indexOf("."));  
  30.             decimalStr = str.substring(str.indexOf(".") + 1);  
  31.         } else if (str.indexOf(".") == 0) {  
  32.             integerStr = "";  
  33.             decimalStr = str.substring(1);  
  34.         } else {  
  35.             integerStr = str;  
  36.             decimalStr = "";  
  37.         }  
  38.         // integerStr去掉首0,不必去掉decimalStr的尾0(超出部分舍去)  
  39.         if (!integerStr.equals("")) {  
  40.             integerStr = Long.toString(Long.parseLong(integerStr));  
  41.             if (integerStr.equals("0")) {  
  42.                 integerStr = "";  
  43.             }  
  44.         }  
  45.         // overflow超出处理能力,直接返回  
  46.         if (integerStr.length() > IUNIT.length) {  
  47.             System.out.println(str + ":超出处理能力");  
  48.             return str;  
  49.         }  
  50.   
  51.         int[] integers = toArray(integerStr);// 整数部分数字  
  52.         boolean isMust5 = isMust5(integerStr);// 设置万单位  
  53.         int[] decimals = toArray(decimalStr);// 小数部分数字  
  54.         return getChineseInteger(integers, isMust5)  
  55.                 + getChineseDecimal(decimals);  
  56.     }  
  57.   
  58.     /** 
  59.      * 整数部分和小数部分转换为数组,从高位至低位 
  60.      */  
  61.     private static int[] toArray(String number) {  
  62.         int[] array = new int[number.length()];  
  63.         for (int i = 0; i < number.length(); i++) {  
  64.             array[i] = Integer.parseInt(number.substring(i, i + 1));  
  65.         }  
  66.         return array;  
  67.     }  
  68.   
  69.     /** 
  70.      * 得到中文金额的整数部分。 
  71.      */  将夜www.jiangyea.com
  72.     private static String getChineseInteger(int[] integers, boolean isMust5) {  
  73.         StringBuffer chineseInteger = new StringBuffer("");  
  74.         int length = integers.length;  
  75.         for (int i = 0; i < length; i++) {  
  76.             // 0出现在关键位置:1234(万)5678(亿)9012(万)3456(元)  
  77.             // 特殊情况:10(拾元、壹拾元、壹拾万元、拾万元)  
  78.             String key = "";  
  79.             if (integers[i] == 0) {  
  80.                 if ((length - i) == 13)// 万(亿)(必填)  
  81.                     key = IUNIT[4];  
  82.                 else if ((length - i) == 9)// 亿(必填)  
  83.                     key = IUNIT[8];  
  84.                 else if ((length - i) == 5 && isMust5)// 万(不必填)  
  85.                     key = IUNIT[4];  
  86.                 else if ((length - i) == 1)// 元(必填)  
  87.                     key = IUNIT[0];  
  88.                 // 0遇非0时补零,不包含最后一位  
  89.                 if ((length - i) > 1 && integers[i + 1] != 0)  
  90.                     key += NUMBERS[0];  
  91.             }  
  92.             chineseInteger.append(integers[i] == 0 ? key  
  93.                     : (NUMBERS[integers[i]] + IUNIT[length - i - 1]));  
  94.         }  
  95.         return chineseInteger.toString();  
  96.     }  
  97.   
  98.     /** 
  99.      * 得到中文金额的小数部分。 
  100.      */  
  101.     private static String getChineseDecimal(int[] decimals) {  
  102.         StringBuffer chineseDecimal = new StringBuffer("");  
  103.         for (int i = 0; i < decimals.length; i++) {  
  104.             // 舍去3位小数之后的  
  105.             if (i == 3)  
  106.                 break;  
  107.             chineseDecimal.append(decimals[i] == 0 ? ""  
  108.                     : (NUMBERS[decimals[i]] + DUNIT[i]));  
  109.         }  
  110.         return chineseDecimal.toString();  
  111.     }  
  112.   
  113.     /** 
  114.      * 判断第5位数字的单位"万"是否应加。 
  115.      */  
  116.     private static boolean isMust5(String integerStr) {  
  117.         int length = integerStr.length();  
  118.         if (length > 4) {  
  119.             String subInteger = "";  
  120.             if (length > 8) {  
  121.                 // 取得从低位数,第5到第8位的字串  
  122.                 subInteger = integerStr.substring(length - 8, length - 4);  
  123.             } else {  
  124.                 subInteger = integerStr.substring(0, length - 4);  
  125.             }  
  126.             return Integer.parseInt(subInteger) > 0;  
  127.         } else {  
  128.             return false;  
  129.         }  
  130.     }  
  131.   
  132.     public static void main(String[] args) {  
  133.         MoneyUtil moneyUtil = new MoneyUtil();  
  134.         System.out.println(moneyUtil.toChinese("5000.23"));  
  135.     }  
  136.   
  137. }  
posted @ 2013-09-28 10:46  将夜  阅读(7003)  评论(0编辑  收藏  举报