Java版数字金额转中文大写_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Java版数字金额转中文大写

Java版数字金额转中文大写

 2011/10/6 8:12:02  MeltingSnower  http://meltingsnower.iteye.com  我要评论(0)
  • 摘要:数据金额转大写,后面的测试数据来源于google,但是和我处理的结果有点儿出入,例如:1001001001MySelf:拾亿零壹佰万零壹仟零壹圆整Google:拾亿零壹佰万壹仟零壹圆整1001001MySelf:壹佰万零壹仟零壹圆整Google:壹佰万壹仟零壹圆整可能是理解上的问题吧,我固执的认为数字间只要有0,这个“零”应该读出来的。packagecom.koeiol.grails;importjava.math.BigDecimal;importjava.text
  • 标签:Java
数据金额转大写,后面的测试数据来源于google,但是和我处理的结果有点儿出入,例如:
1001001001
MySelf:拾亿零壹佰万零壹仟零壹圆整
Google:拾亿零壹佰万壹仟零壹圆整
1001001
MySelf:壹佰万零壹仟零壹圆整
Google:壹佰万壹仟零壹圆整

可能是理解上的问题吧,我固执的认为数字间只要有0,这个“零”应该读出来的。

package com.koeiol.grails;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * 数字金额转大写
 * 
 * 上限:16个9 .99
 * 
 * @author MeltingSnower@hotmail.com
 * 
 */
public class ChineseMoney {

	private static BigDecimal MAX_VALUE = new BigDecimal("9999999999999999.99");
	private static BigDecimal MIN_VALUE = new BigDecimal("0.01");
	private static String FORMAT_PATTERN = "0000000000000000.00";

	private static String[] CHINESE_NUMBER = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
	private static String[][] CHINESE_UNIT = { { "", "拾", "佰", "仟" }, { "兆", "亿", "万", "" }, { "角", "分" } };

	private static String[] BASE_UNIT = { "圆", "整" };

	public static String convert(BigDecimal convertValue) {
		if (convertValue == null || convertValue.compareTo(BigDecimal.ZERO) == 0)
			return CHINESE_NUMBER[0] + BASE_UNIT[0] + BASE_UNIT[1];
		if (convertValue.compareTo(MAX_VALUE) > 0 || convertValue.compareTo(MIN_VALUE) < 0)
			return convertValue.toString();
		String[] parts = new DecimalFormat(FORMAT_PATTERN).format(convertValue).split("\\.");
		String result = "";
		boolean start = false, zero = false, zeroed = false;
		for (int i = 0; i < 4; i++) {
			int groupValue = Integer.parseInt(parts[0].substring(i * 4, (i + 1) * 4));
			if (start)
				if ((zero || groupValue < 1000) && !zeroed) {
					result += CHINESE_NUMBER[0];
					zeroed = true;
				}
			if (groupValue > 0) {
				result += groupConvert(groupValue) + CHINESE_UNIT[1][i];
				start = true;
				zeroed = false;
			}
			if (groupValue > 0 && groupValue % 10 == 0)
				zero = true;
			else
				zero = false;
		}
		if (convertValue.compareTo(BigDecimal.ONE) > -1)
			result += BASE_UNIT[0];
		if (!parts[1].equals("00")) {
			if (result.length() > 0 && (parts[0].charAt(parts[0].length() - 1) == '0' || parts[1].charAt(0) == '0'))
				result += CHINESE_NUMBER[0];
			for (int i = 0; i < parts[1].length(); i++) {
				int single = parts[1].charAt(i) - '0';
				if (single > 0)
					result += CHINESE_NUMBER[single] + CHINESE_UNIT[2][i];
			}
		} else
			result += BASE_UNIT[1];
		return result;

	}

	private static String groupConvert(int groupValue) {
		if (groupValue > 9 && groupValue < 20)
			return CHINESE_UNIT[0][1] + (groupValue % 10 > 0 ? CHINESE_NUMBER[groupValue % 10] : "");
		String stringValue = String.valueOf(groupValue), result = "";
		boolean valid = false, zero = false, zerod = false;
		for (int i = 0; i < stringValue.length(); i++) {
			int intValue = stringValue.charAt(stringValue.length() - i - 1) - '0';
			if (intValue > 0) {
				valid = true;
				zero = false;
				zerod = false;
			} else if (valid)
				zero = true;
			if (valid) {
				if (!zerod)
					result = CHINESE_NUMBER[intValue] + (zero ? "" : CHINESE_UNIT[0][i]) + result;
				if (zero)
					zerod = true;
			}
		}
		return result;
	}

	public static void main(String[] args) {
		Map<String, String> testCaseMap = new HashMap<String, String>();
		testCaseMap.put("0", "零圆整");
		testCaseMap.put("1", "壹圆整");
		testCaseMap.put("12", "拾贰圆整");
		testCaseMap.put("123", "壹佰贰拾叁圆整");
		testCaseMap.put("1234", "壹仟贰佰叁拾肆圆整");
		testCaseMap.put("12345", "壹万贰仟叁佰肆拾伍圆整");
		testCaseMap.put("123456", "拾贰万叁仟肆佰伍拾陆圆整");
		testCaseMap.put("1234567", "壹佰贰拾叁万肆仟伍佰陆拾柒圆整");
		testCaseMap.put("12345678", "壹仟贰佰叁拾肆万伍仟陆佰柒拾捌圆整");
		testCaseMap.put("123456789", "壹亿贰仟叁佰肆拾伍万陆仟柒佰捌拾玖圆整");
		testCaseMap.put("1234567890", "拾贰亿叁仟肆佰伍拾陆万柒仟捌佰玖拾圆整");
		testCaseMap.put("11", "拾壹圆整");
		testCaseMap.put("101", "壹佰零壹圆整");
		testCaseMap.put("1001", "壹仟零壹圆整");
		testCaseMap.put("10001", "壹万零壹圆整");
		testCaseMap.put("100001", "拾万零壹圆整");
		testCaseMap.put("1000001", "壹佰万零壹圆整");
		testCaseMap.put("10000001", "壹仟万零壹圆整");
		testCaseMap.put("100000001", "壹亿零壹圆整");
		testCaseMap.put("1000000001", "拾亿零壹圆整");
		testCaseMap.put("10000000001", "壹佰亿零壹圆整");
		testCaseMap.put("10101", "壹万零壹佰零壹圆整");
		testCaseMap.put("1010101", "壹佰零壹万零壹佰零壹圆整");
		testCaseMap.put("101010101", "壹亿零壹佰零壹万零壹佰零壹圆整");
		testCaseMap.put("10101010101", "壹佰零壹亿零壹佰零壹万零壹佰零壹圆整");
		testCaseMap.put("1001001", "壹佰万壹仟零壹圆整");
		testCaseMap.put("1001001001", "拾亿零壹佰万壹仟零壹圆整");
		testCaseMap.put("100010001", "壹亿零壹万零壹圆整");
		testCaseMap.put("10000100001", "壹佰亿零拾万零壹圆整");
		testCaseMap.put("1000001000001", "壹兆零壹佰万零壹圆整");
		testCaseMap.put("0.01", "壹分");
		testCaseMap.put("0.1", "壹角");
		testCaseMap.put("0.11", "壹角壹分");
		testCaseMap.put("1.01", "壹圆零壹分");
		testCaseMap.put("1.1", "壹圆壹角");
		testCaseMap.put("1.11", "壹圆壹角壹分");
		testCaseMap.put("10.01", "拾圆零壹分");
		testCaseMap.put("10.1", "拾圆零壹角");
		testCaseMap.put("10.11", "拾圆零壹角壹分");

		for (Entry<String, String> testItem : testCaseMap.entrySet())
			if (!convert(new BigDecimal(testItem.getKey())).equals(testItem.getValue()))
				System.out.println(testItem.getKey() + "\nMySelf:" + convert(new BigDecimal(testItem.getKey())) + "\nGoogle:" + testItem.getValue());

	}
}

发表评论
用户名: 匿名