Эх сурвалжийг харах

修改中文金额的处理方式

sunyj 9 жил өмнө
parent
commit
8cbeb61fca

+ 127 - 98
src/main/java/com/uas/report/util/ChineseMoneyUtils.java

@@ -9,12 +9,6 @@ import java.math.BigDecimal;
  * @since 2016年9月27日 下午8:11:04
  */
 public class ChineseMoneyUtils {
-	/** 定义数组存放数字对应的大写 */
-	private final static String[] STR_NUMBER = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
-
-	/** 定义数组存放位数的大写 */
-	private final static String[] STR_MODIFY = { "", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟" };
-
 	/**
 	 * 将一个数字转化为金额
 	 * 
@@ -23,123 +17,158 @@ public class ChineseMoneyUtils {
 	 * @return 返一个转换好的字符串
 	 */
 	public static String numberToChinese(double tempNumber) {
-		java.text.DecimalFormat df = new java.text.DecimalFormat("#.#########");
-		String pTemp = String.valueOf(df.format(tempNumber));
-		StringBuffer sbResult = new StringBuffer(
-				getSign(pTemp) + getInteger(pTemp) + getDot(pTemp) + getFraction(pTemp));
-		return sbResult.toString();
+		String result = cleanZero(splitNum(round(tempNumber)));
+		if (!result.contains("分") && !result.contains("角")) {
+			result += "整";
+		}
+		return result;
 	}
 
 	public static String numberToChinese(BigDecimal tempNumber) {
+		if (tempNumber == null) {
+			return null;
+		}
 		return numberToChinese(tempNumber.doubleValue());
 	}
 
 	/**
-	 * 转化整数部分
+	 * 把用户输入的数以小数点为界分割开来,并调用 numFormat() 方法 进行相应的中文金额大写形式的转换 注:传入的这个数应该是经过
+	 * roundString() 方法进行了四舍五入操作的
 	 * 
-	 * @param tempString
-	 * @return 返回整数部分
+	 * @param s
+	 *            String
+	 * @return 转换好的中文金额大写形式的字符串
 	 */
-	private static String getInteger(String tempString) {
-		/** 用来保存整数部分数字串 */
-		String strInteger = null;//
-		/** 记录"."所在位置 */
-		int intDotPos = tempString.indexOf(".");
-		int intSignPos = tempString.indexOf("-");
-		if (intDotPos == -1)
-			intDotPos = tempString.length();
-		/** 取出整数部分 */
-		strInteger = tempString.substring(intSignPos + 1, intDotPos);
-		strInteger = new StringBuffer(strInteger).reverse().toString();
-		StringBuffer sbResult = new StringBuffer();
-		for (int i = 0; i < strInteger.length(); i++) {
-			sbResult.append(STR_MODIFY[i]);
-			sbResult.append(STR_NUMBER[strInteger.charAt(i) - 48]);
-		}
-
-		sbResult = sbResult.reverse();
-		replace(sbResult, "零拾", "零");
-		replace(sbResult, "零佰", "零");
-		replace(sbResult, "零仟", "零");
-		replace(sbResult, "零万", "万");
-		replace(sbResult, "零亿", "亿");
-		replace(sbResult, "零零", "零");
-		replace(sbResult, "零零零", "零");
-		/** 这两句不能颠倒顺序 */
-		replace(sbResult, "零零零零万", "");
-		replace(sbResult, "零零零零", "");
-		/** 这样读起来更习惯. */
-		replace(sbResult, "壹拾亿", "拾亿");
-		replace(sbResult, "壹拾万", "拾万");
-		/** 删除个位上的零 */
-		if (sbResult.charAt(sbResult.length() - 1) == '零' && sbResult.length() != 1)
-			sbResult.deleteCharAt(sbResult.length() - 1);
-		if (strInteger.length() == 2) {
-			replace(sbResult, "壹拾", "拾");
+	private static String splitNum(String s) {
+		// 如果传入的是空串则继续返回空串
+		if ("".equals(s)) {
+			return "";
 		}
-		/** 将结果反转回来. */
-		return sbResult.toString();
+		// 以小数点为界分割这个字符串
+		int index = s.indexOf(".");
+		// 截取并转换这个数的整数部分
+		String intOnly = s.substring(0, index);
+		String part1 = numFormat(1, intOnly);
+		// 截取并转换这个数的小数部分
+		String smallOnly = s.substring(index + 1);
+		String part2 = numFormat(2, smallOnly);
+		// 把转换好了的整数部分和小数部分重新拼凑一个新的字符串
+		return part1 + part2;
 	}
 
 	/**
-	 * 转化小数部分 例:输入22.34返回叁肆
+	 * 对传入的数进行四舍五入操作
 	 * 
-	 * @param tempString
-	 * @return
+	 * @param tempNumber
+	 * @return 四舍五入后的新值
 	 */
-	private static String getFraction(String tempString) {
-		String strFraction = null;
-		int intDotPos = tempString.indexOf(".");
-		/** 没有点说明没有小数,直接返回 */
-		if (intDotPos == -1)
-			return "元整";
-		strFraction = tempString.substring(intDotPos + 1);
-		StringBuffer sbResult = new StringBuffer(strFraction.length());
-		for (int i = 0; i < strFraction.length(); i++) {
-			sbResult.append(STR_NUMBER[strFraction.charAt(i) - 48]);
+	private static String round(double tempNumber) {
+		// 此操作作用在小数点后两位上
+		tempNumber = (tempNumber * 100 + 0.5) / 100;
+		// 将 d 进行格式化
+		String s = new java.text.DecimalFormat("##0.000").format(tempNumber);
+		// 以小数点为界分割这个字符串
+		int index = s.indexOf(".");
+		// 这个数的整数部分
+		String intOnly = s.substring(0, index);
+		// 规定数值的最大长度只能到万亿单位,否则返回 "0"
+		if (intOnly.length() > 13) {
+			System.out.println("输入数据过大!(整数部分最多13位!)");
+			return "";
 		}
-		return sbResult.toString();
-	}
-
-	/**
-	 * 判断传入的字符串中是否有.如果有则返回点
-	 * 
-	 * @param tempString
-	 * @return
-	 */
-	private static String getDot(String tempString) {
-		return tempString.indexOf(".") != -1 ? "点" : "";
+		// 这个数的小数部分
+		String smallOnly = s.substring(index + 1);
+		// 如果小数部分大于两位,只截取小数点后两位
+		if (smallOnly.length() > 2) {
+			String roundSmall = smallOnly.substring(0, 2);
+			// 把整数部分和新截取的小数部分重新拼凑这个字符串
+			s = intOnly + "." + roundSmall;
+		}
+		return s;
 	}
 
 	/**
-	 * 判断传入的字符串中是否有-如果有则返回负
+	 * 把传入的数转换为中文金额大写形式
 	 * 
-	 * @param tempString
-	 * @return
+	 * @param flag
+	 *            int 标志位,1 表示转换整数部分,0 表示转换小数部分
+	 * @param s
+	 *            String 要转换的字符串
+	 * @return 转换好的带单位的中文金额大写形式
 	 */
-	private static String getSign(String tempString) {
-		return tempString.indexOf("-") != -1 ? "负" : "";
+	private static String numFormat(int flag, String s) {
+		int sLength = s.length();
+		// 货币大写形式
+		String bigLetter[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
+		// 货币单位
+		String unit[] = { "元", "拾", "佰", "仟", "万",
+				// 拾万位到仟万位
+				"拾", "佰", "仟",
+				// 亿位到万亿位
+				"亿", "拾", "佰", "仟", "万" };
+		String small[] = { "分", "角" };
+		// 用来存放转换后的新字符串
+		String newS = "";
+		// 逐位替换为中文大写形式
+		for (int i = 0; i < sLength; i++) {
+			if (flag == 1) {
+				// 转换整数部分为中文大写形式(带单位)
+				newS = newS + bigLetter[s.charAt(i) - 48] + unit[sLength - i - 1];
+			} else if (flag == 2) {
+				// 转换小数部分(带单位)
+				newS = newS + bigLetter[s.charAt(i) - 48] + small[sLength - i - 1];
+			}
+		}
+		return newS;
 	}
 
 	/**
-	 * 替代字符
+	 * 把已经转换好的中文金额大写形式加以改进,清理这个字 符串里面多余的零,让这个字符串变得更加可观 注:传入的这个数应该是经过 splitNum()
+	 * 方法进行处理,这个字 符串应该已经是用中文金额大写形式表示的
 	 * 
-	 * @param pValue
-	 * @param pSource
-	 * @param pDest
+	 * @param s
+	 *            String 已经转换好的字符串
+	 * @return 改进后的字符串
 	 */
-	private static void replace(StringBuffer pValue, String pSource, String pDest) {
-		if (pValue == null || pSource == null || pDest == null)
-			return;
-		/** 记录pSource在pValue中的位置 */
-		int intPos = 0;
-		do {
-			intPos = pValue.toString().indexOf(pSource);
-			/** 没有找到pSource */
-			if (intPos == -1)
-				break;
-			pValue.delete(intPos, intPos + pSource.length());
-			pValue.insert(intPos, pDest);
-		} while (true);
+	private static String cleanZero(String s) {
+		// 如果传入的是空串则继续返回空串
+		if ("".equals(s)) {
+			return "";
+		}
+		// 如果用户开始输入了很多 0 去掉字符串前面多余的'零',使其看上去更符合习惯
+		while (s.charAt(0) == '零') {
+			// 将字符串中的 "零" 和它对应的单位去掉
+			s = s.substring(2);
+			// 如果用户当初输入的时候只输入了 0,则只返回一个 "零"
+			if (s.length() == 0) {
+				return "零";
+			}
+		}
+		// 字符串中存在多个'零'在一起的时候只读出一个'零',并省略多余的单位
+		/* 由于本人对算法的研究太菜了,只能用4个正则表达式去转换了,各位大虾别介意哈... */
+		String regex1[] = { "零仟", "零佰", "零拾" };
+		String regex2[] = { "零亿", "零万", "零元" };
+		String regex3[] = { "亿", "万", "元" };
+		String regex4[] = { "零角", "零分" };
+		// 第一轮转换把 "零仟", 零佰","零拾"等字符串替换成一个"零"
+		for (int i = 0; i < 3; i++) {
+			s = s.replaceAll(regex1[i], "零");
+		}
+		// 第二轮转换考虑 "零亿","零万","零元"等情况
+		// "亿","万","元"这些单位有些情况是不能省的,需要保留下来
+		for (int i = 0; i < 3; i++) {
+			// 当第一轮转换过后有可能有很多个零叠在一起
+			// 要把很多个重复的零变成一个零
+			s = s.replaceAll("零零零", "零");
+			s = s.replaceAll("零零", "零");
+			s = s.replaceAll(regex2[i], regex3[i]);
+		}
+		// 第三轮转换把"零角","零分"字符串省略
+		for (int i = 0; i < 2; i++) {
+			s = s.replaceAll(regex4[i], "");
+		}
+		// 当"万"到"亿"之间全部是"零"的时候,忽略"亿万"单位,只保留一个"亿"
+		s = s.replaceAll("亿万", "亿");
+		return s;
 	}
 }