当前位置 : 主页 > 编程语言 > java >

阿拉伯数字金额小写转大写

来源:互联网 收集:自由互联 发布时间:2021-06-28
gistfile1.txt public static String digitUppercase(double n){String fraction[] = {"角", "分"};String digit[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };String unit[][] = {{"元", "万", "亿"},{"", "拾", "佰
gistfile1.txt
public static String digitUppercase(double n){
		String fraction[] = {"角", "分"};
		String digit[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
		String unit[][] = {{"元", "万", "亿"},
				{"", "拾", "佰", "仟"}};

		String head = n < 0? "负": "";
		n = Math.abs(n);

		String s = "";
		for (int i = 0; i < fraction.length; i++) {
			s += (digit[(int)(Math.floor(accuracyByMultiply(10.0, accuracyByMultiply(n, Math.pow(10, i)))) % 10)] + fraction[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 = digit[integerPart%10]+unit[1][j] + p;
				integerPart = integerPart/10;
			}
			s = p.replaceAll("(零.)*零$", "").replaceAll("^$", "零") + unit[0][i] + s;
		}
		return head + s.replaceAll("(零.)*零元", "元").replaceFirst("(零.)+", "").replaceAll("(零.)+", "零").replaceAll("^整$", "零元整");
	}

public static Double accuracyByMultiply(Double v1, Double v2){
		BigDecimal b1 = new BigDecimal(Double.toString(v1));   
		BigDecimal b2 = new BigDecimal(Double.toString(v2));   
		return b1.multiply(b2).doubleValue();
	}
网友评论