首页>代码>java金额转换工具类>/parsemoney/src/ParseMoney.java
public class ParseMoney {

	static final String[] item = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌",
			"玖" };
	static final String number[] = { "元", "拾", "佰", "仟", "万", "拾", "百", "仟",
			"亿", "拾", "佰", "仟", "万", "拾", "佰", "仟","万","拾","百","千" };
	static final String[] sec = { "角", "分" };

	/**
	 * 判断是否为有效金额
	 * 
	 * @param item
	 * @return
	 */
	boolean isRight(String item) {

		if (item.charAt(0) == '0' || item.charAt(0) == '.') {
			return false;
		}
		char[] c = item.toCharArray();
		int count = 0;
		for (int index = 1; index < item.length(); index++) {
			if (c[index] == '.') {
				count++;
				continue;
			}
			if (c[index] < '0' || c[index] > '9') {
				return false;
			}

		}
		if (count > 1) {
			return false;
		}
		return true;
	}

	String parseToMoney(String money) {
		StringBuilder sbd = new StringBuilder();
		if (!isRight(money)) {
			return "";
		}
		int index = money.indexOf('.');
		String first = (index == -1) ? money : money.substring(0, index);
		String second = (index == -1) ? "" : money.substring(index + 1, money
				.length());
		char[] firstItem = first.toCharArray();
		for (int i = 0, len = firstItem.length; i < len; i++) {
			int size = sbd.toString().length();
			int itemIndex = Integer.parseInt(firstItem[i] + "");
			if (i > 0 && i < len - 1 && firstItem[i] == '0') {
				if ((number[len - i - 1] == "亿")) {
					sbd.append(number[len - i - 1]);

				}
				if ((number[len - i - 1] == "万")) {
					if (sbd.toString().charAt(size - 1) != '亿') {
						sbd.append(number[len - i - 1]);
					}
				}

				else if (firstItem[i + 1] != '0') {
					sbd.append("零");
				}

			} else if (i == len - 1 && firstItem[i] == '0') {
				sbd.append(number[len - i - 1]);
			} else {
				sbd.append(item[itemIndex]);
				sbd.append(number[len - i - 1]);
			}

		}

		if (second != "") {
			sbd.append("零");
			for (int y = 0; y < second.length(); y++) {
				int itemIndex = Integer.parseInt(second.charAt(y) + "");
				if (itemIndex != 0) {
					sbd.append(item[itemIndex]);
					sbd.append(sec[y]);
				}
			}
		}

		return sbd.toString();
	}

	public static void main(String[] args) {

		System.out.println(new ParseMoney().parseToMoney("188292300003.34"));
		System.out.println(new ParseMoney().parseToMoney("161010211.34"));
		System.out.println(new ParseMoney().parseToMoney("100000011.89"));
		System.out.println(new ParseMoney().parseToMoney("10000000.12"));
		System.out.println(new ParseMoney().parseToMoney("1000000102301.78"));

		System.out.println(new ParseMoney().parseToMoney("9867867000123.78"));
	}

}
最近下载更多
jinbaotest  LV1 2023年4月13日
陌上花开yue  LV2 2020年8月27日
jkchen1  LV6 2020年6月5日
故事_sun  LV26 2018年5月28日
HermC_  LV2 2017年11月21日
最代码官方  LV167 2012年11月17日
最近浏览更多
fesfefe  LV13 2023年11月11日
多吃多占  LV12 2023年10月20日
jinbaotest  LV1 2023年4月13日
xingxing1234  LV10 2023年3月22日
crosa_Don  LV18 2022年7月6日
123q45  LV1 2021年8月5日
mm2013  LV5 2021年7月16日
jkchen1  LV6 2020年6月5日
hfk2020  LV2 2020年5月22日
czx5450  LV16 2020年2月28日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友