1. 程式人生 > >Java小寫金額轉換大寫與金額比對(支援繁體大寫金額)

Java小寫金額轉換大寫與金額比對(支援繁體大寫金額)

package com.gerrard.pmc.util;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

/**
 * 金額工具類
 *
 * @author GERRARD 
 */
public final class MoneyUtil {

	/**
	 * 大寫數字 
	 */
	private static final String[] NUMBERS = { "零", "壹", "貳", "叄", "肆", "伍",
			"陸", "柒", "捌", "玖" };

	/**
	 * 整數單位
	 */
	private static final String[] IUNIT = { "元", "拾", "佰", "仟", "萬", "拾", "佰",
			"仟", "億", "拾", "佰", "仟", "萬", "拾", "佰", "仟" };

	/**
	 * 小數單位
	 */
	private static final String[] DUNIT = { "角", "分", "釐" };

	/**
	 * 大寫金額
	 * @param str
	 * @return
	 */
	public static String toChinese(String str) 
	{
		str = str.replaceAll(",", "");// 去掉","
		
		String integerStr;// 整數部分數字
		
		String decimalStr;// 小數部分數字

		// 初始化:分離整數部分和小數部分
		if (str.indexOf(".") > 0) 
		{
			integerStr = str.substring(0, str.indexOf("."));
			
			decimalStr = str.substring(str.indexOf(".") + 1);
			
		} else if (str.indexOf(".") == 0) 
		{
			integerStr = "";
			decimalStr = str.substring(1);
			
		} else 
		{
			integerStr = str;
			decimalStr = "";
		}
		
		// integerStr去掉首0,不必去掉decimalStr的尾0(超出部分捨去)
		if (!integerStr.equals("")) 
		{
			integerStr = Long.toString(Long.parseLong(integerStr));
			
			if (integerStr.equals("0")) 
			{
				integerStr = "";
			}
		}
		
		// overflow超出處理能力,直接返回
		if (integerStr.length() > IUNIT.length) 
		{
			System.out.println(str + ":超出處理能力");
			return str;
		}

		int[] integers = toArray(integerStr);// 整數部分數字
		
		boolean isMust5 = isMust5(integerStr);// 設定萬單位
		
		int[] decimals = toArray(decimalStr);// 小數部分數字
		
		return getChineseInteger(integers, isMust5)
				+ getChineseDecimal(decimals);
	}

	/**
	 * 整數部分和小數部分轉換為陣列,從高位至低位
	 * @param number
	 * @return
	 */
	private static int[] toArray(String number) 
	{
		int[] array = new int[number.length()];
		
		for (int i = 0; i < number.length(); i++) 
		{
			array[i] = Integer.parseInt(number.substring(i, i + 1));
		}
		return array;
	}

	/**
	 * 中文整數金額
	 * @param integers
	 * @param isMust5
	 * @return
	 */
	private static String getChineseInteger(int[] integers, boolean isMust5) 
	{
		StringBuffer chineseInteger = new StringBuffer("");
		
		int length = integers.length;
		
		for (int i = 0; i < length; i++) 
		{
			// 0出現在關鍵位置:1234(萬)5678(億)9012(萬)3456(元)
			// 特殊情況:10(拾元、壹拾元、壹拾萬元、拾萬元)
			String key = "";
			
			if (integers[i] == 0) 
			{
				if ((length - i) == 13)// 萬(億)(必填)
				{
					key = IUNIT[4];
				}
					
				else if ((length - i) == 9)// 億(必填)
				{
					key = IUNIT[8];
				}
					
				else if ((length - i) == 5 && isMust5)// 萬(不必填)
				{
					key = IUNIT[4];
				}
					
				else if ((length - i) == 1)// 元(必填) 
				{
					key = IUNIT[0];
				}
					
				// 0遇非0時補零,不包含最後一位
				if ((length - i) > 1 && integers[i + 1] != 0)
				{
					key += NUMBERS[0];
				}
					
			}
			chineseInteger.append(integers[i] == 0 ? key
					: (NUMBERS[integers[i]] + IUNIT[length - i - 1]));
		}
		return chineseInteger.toString();
	}

	/**
	 * 中文小數金額
	 * @param decimals
	 * @return
	 */
	private static String getChineseDecimal(int[] decimals) 
	{
		StringBuffer chineseDecimal = new StringBuffer("");
		
		for (int i = 0; i < decimals.length; i++) 
		{
			// 捨去3位小數之後的
			if (i == 3)
				break;
			
			chineseDecimal.append(decimals[i] == 0 ? ""
					: (NUMBERS[decimals[i]] + DUNIT[i]));
		}
		return chineseDecimal.toString();
	}

	/**
	 * 第五位數字是否加萬
	 * @param integerStr
	 * @return
	 */
	private static boolean isMust5(String integerStr) 
	{
		int length = integerStr.length();
		
		if (length > 4) 
		{
			String subInteger = "";
			
			if (length > 8) 
			{
				// 取得從低位數,第5到第8位的字串
				subInteger = integerStr.substring(length - 8, length - 4);
			} else 
			{
				subInteger = integerStr.substring(0, length - 4);
			}
			return Integer.parseInt(subInteger) > 0;
		} else 
		{
			return false;
		}
	}

	/**
	 * 大小寫金額比較 支援最小單位:釐
	 * 
	 * @param amount
	 *            小寫金額
	 * @param chineseAmount
	 *            大寫金額
	 * @return
	 */
	public static boolean isAmtEqChn(String amount, String chineseAmount) {

		if (StringUtils.isBlank(chineseAmount)) 
		{
			return false;
		}

		// 格式化大寫金額去整
		chineseAmount = chineseAmount.replace("整", "");
		chineseAmount = chineseAmount.replace("正", "");
		chineseAmount = chineseAmount.replace("人民幣", "");

		chineseAmount = toSimplified(chineseAmount);

		String toChnAmt = toChinese(amount);

		if (chineseAmount.equals(toChnAmt)) 
		{
			return true;
		}
		return false;
	}

	/**
	 * 大寫金額簡體化
	 * 
	 * @param chnAmt
	 * @return
	 */
	public static String toSimplified(String chnAmt) {

		if (StringUtils.isBlank(chnAmt)) 
		{
			return null;
		}

		char[] amtArrChar = chnAmt.toCharArray();

		Map<String, String> mapping = getSimpToTradMapping();

		for (int i = 0; i < amtArrChar.length; i++) 
		{
			if (mapping.containsKey(String.valueOf(amtArrChar[i]))) 
			{
				amtArrChar[i] = mapping.get(String.valueOf(amtArrChar[i]))
						.charAt(0);
			}
		}

		return String.valueOf(amtArrChar);
	}

	/**
	 * 繁體對應
	 * 
	 * @return
	 */
	private static Map<String, String> getSimpToTradMapping() 
	{

		Map<String, String> mapping = new HashMap<String, String>();

		mapping.put("圓", "元");
		mapping.put("圓", "元");
		mapping.put("貳", "貳");
		mapping.put("陸", "陸");
		mapping.put("億", "億");
		mapping.put("萬", "萬");

		return mapping;
	}
	
	public static void main(String[] args) {
		
		String amount = "99999.99";
		
		System.out.println("小寫金額" + amount + "轉換大寫金額:" + toChinese("99999.99"));
		
		String chnAmount = "玖萬玖仟玖佰玖拾玖圓玖角玖分";
		
		System.out.println(chnAmount + "-簡體化:" + toSimplified(chnAmount));

		System.out.println("小寫金額:" + amount + " 與 大寫金額:" + chnAmount + "比較結果--------"+ isAmtEqChn(amount, chnAmount));
	}
}

網上的程式碼,自己又修改添加了一部分