java钱等 数字--大写数字

public String upper(double n) {
		String dec[] = {"角", "分"};//小数部分单位
		String num[] = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
		String unit[][] = {{"元", "万", "亿"}, {"", "拾", "佰", "仟"}};//单位
		String head = n < 0 ? "负" : "";
		n = Math.abs(n);
	 
		String s = "";
		for (int i = 0; i < dec.length; i++) {
			s += (num[(int) (Math.floor(n * 10 * Math.pow(10, i)) % 10)] + dec[i]).replaceAll("(零.)+", "");
		}
		if (s.length() < 1) {
			s = "整";
		}
		int integerPart = (int) Math.floor(n);
	 
		for (int i = 0; i < unit[0].length && integerPart > 0; i++) {
			String p = "";
			for (int j = 0; j < unit[1].length && n > 0; j++) {
				p = num[integerPart % 10] + unit[1][j] + p;
				integerPart = integerPart / 10;
			}
			s = p.replaceAll("(零.)*零$", "").replaceAll("^$", "零") + unit[0][i] + s;
		}
		return head + s.replaceAll("(零.)*零元", "元").replaceFirst("(零.)+", "").replaceAll("(零.)+", "零").replaceAll("^整$", "零元整");
	}
	

开收据或者发票时总是需要提供大写数字,此方法可以 将double类型数字转换成汉字大写数字。如果数据库中的钱数为字符串类型则要先转换成double类型 double n = Double.parseDouble(str);

猜你喜欢

转载自blog.csdn.net/qq_37189793/article/details/81183250