java代码中将金额阿拉伯数字转化为中文大写

代码如下:

/**
	 * 金额大小写转换
	 * 
	 * @param input
	 * @return
	 */
	public static String numtochinese(String input) {
		String s1 = "零壹贰叁肆伍陆柒捌玖";
		String s4 = "分角整元拾佰仟万拾佰仟亿拾佰仟";
		String temp = "";
		String result = "";
		if (input == null) {
			return "输入字串不是数字串只能包括以下字符(´0´~´9´,´.´),输入字串最大只能精确到仟亿,小数点只能两位!";
		}
		temp = input.trim();
		try {
		} catch (Exception e) {
			return "输入字串不是数字串只能包括以下字符(´0´~´9´,´.´),输入字串最大只能精确到仟亿,小数点只能两位!";
		}
		int len = 0;
		if (temp.indexOf(".") == -1) {
			len = temp.length();
		} else {
			len = temp.indexOf(".");
		}
		if (len > s4.length() - 3) {
			return ("输入字串最大只能精确到仟亿,小数点只能两位!");
		}
		int n1;
		String num = "";
		String unit = "";
		for (int i = 0; i < temp.length(); i++) {
			if (i > len + 2) {
				break;
			}
			if (i == len) {
				continue;
			}
			n1 = Integer.parseInt(String.valueOf(temp.charAt(i)));
			num = s1.substring(n1, n1 + 1);
			n1 = len - i + 2;
			unit = s4.substring(n1, n1 + 1);
			result = result.concat(num).concat(unit);
		}
		if ((len == temp.length()) || (len == temp.length() - 1)) {
			result = result.concat("整");
		}
		if (len == temp.length() - 2) {
			result = result.concat("零分");
		}
		return repace_acc(result);
	}

	// 金额小写变大写中小数位
	/**
	 * @param money
	 *            金额数值
	 * @param value
	 *            值
	 */
	public static String repace_acc(String money) {
		String outmoney = "";
		money = money.replace("零分", "");
		money = money.replace("零角", "零");
		outmoney = money;
		int yy = 0;
		while (true) {
			int lett = outmoney.length();
			// 例如:0.35 零元三角五分 对于这种数据 不需要替换
			if (!outmoney.startsWith("零元")) {
				outmoney = outmoney.replace("零元", "元");
			}
			if (!outmoney.startsWith("零万")) {
				outmoney = outmoney.replace("零万", "万");
			}
			if (!outmoney.startsWith("零亿")) {
				outmoney = outmoney.replace("零亿", "亿");
			}
			if (!outmoney.startsWith("零仟")) {
				outmoney = outmoney.replace("零仟", "零");
			}
			if (!outmoney.startsWith("零佰")) {
				outmoney = outmoney.replace("零佰", "零");
			}
			if (!outmoney.startsWith("零零")) {
				outmoney = outmoney.replace("零零", "零");
			}
			if (!outmoney.startsWith("零拾")) {
				outmoney = outmoney.replace("零拾", "零");
			}
			if (!outmoney.startsWith("亿万")) {
				outmoney = outmoney.replace("亿万", "亿零");
			}
			if (!outmoney.startsWith("万仟")) {
				outmoney = outmoney.replace("万仟", "万零");
			}
			if (!outmoney.startsWith("仟佰")) {
				outmoney = outmoney.replace("仟佰", "仟零");
			}
			yy = outmoney.length();
			if (yy == lett) {
				break;
			}
		}
		yy = outmoney.length();
		if (outmoney.charAt(yy - 1) == '零') {
			outmoney = outmoney.substring(0, yy - 1);
		}
		yy = outmoney.length();
		if (outmoney.charAt(yy - 1) == '元') {
			outmoney = outmoney + '整';
		}
		System.out.println(outmoney);
		return outmoney;
	}

这个是测试类:

public static void main(String args[]) throws IOException {
		
		
		/**************** 测试金额人民币大写转换begin ******************************/
		numtochinese("15866.36");
		System.out.println(numtochinese("15866.36"));
		/**************** 测试金额人民币大写转换end *********************************/
		

	}
测试结果:


  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值