把数字转换成人民币大写的形式Java类详解编程语言

/**  
 * @author sunpander  
  
 * 把输入的数字转换成人民币大写的形式  
 */ 
 
import java.io.BufferedReader; 
import java.io.InputStreamReader; 
 
public class ChangeNumToRMB { 
 
	String moneyStrs[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; 
 
	public static void main(String[] args) { 
		// TODO Auto-generated method stub 
		boolean isDebug = true; 
		while (isDebug) { 
			int num = 0; 
			String changedStr = ""; 
			System.out.println("请输入需要转换的金额:"); 
			BufferedReader br = new BufferedReader(new InputStreamReader( 
					System.in)); 
			boolean isWrongInput = true; 
			while (isWrongInput) { 
				try { 
					String str = br.readLine(); 
 
					num = Integer.parseInt(str); 
 
				} catch (Exception e) { 
					System.out.println("输入数字有误,必须是:0" + "到" + Integer.MAX_VALUE 
							+ " 的整数"); 
 
					// TODO Auto-generated catch block 
					// e.printStackTrace(); 
					isWrongInput = true; 
					continue; 
				} 
				isWrongInput = false; 
			} 
			ChangeNumToRMB chan = new ChangeNumToRMB(); 
			changedStr = chan.getChangedString(num); 
		} 
	} 
 
	public String getChangedString(int num) { 
 
		// Money用于存放输入的金额数 
		int moneyNum = num; 
		String moneyStr = ""; 
		System.out.println("输入的钱数是:"); 
		if (moneyNum < 0) { 
			System.out.println("数字必须大于零"); 
		} else if (moneyNum == 0) { 
			moneyStr = "零元"; 
		} 
		// 只有四位数,即最大9999.小于10000 
		else if (moneyNum < 10000) { 
			moneyStr = this.getFourNumStr(moneyNum) + "元"; 
		} 
		// 大于一万,小于一千万 
		else if (moneyNum <= 10000000)// && moneyNum>10000) 
		{ 
 
			int firstFourNum, lastFourNum; 
			firstFourNum = moneyNum / 10000; 
			lastFourNum = moneyNum % 10000; 
			String firstFourStr = this.getFourNumStr(firstFourNum); 
			String lastFourStr = this.getFourNumStr(lastFourNum); 
			if (!lastFourStr.contains("仟")) 
				lastFourStr = "零" + lastFourStr; 
			moneyStr = firstFourStr + "万" + lastFourStr + "元"; 
		} 
		// 大于一千万.即上亿但小于最大整数 
		else if (moneyNum < Integer.MAX_VALUE)// && moneyNum>10000000) 
		{ 
			int firstFourNum, secondFourNum, lastFourNum; 
			firstFourNum = moneyNum / 100000000; 
			secondFourNum = (moneyNum / 10000) % 1000; 
			lastFourNum = moneyNum % 10000; 
			String firstFourStr = this.getFourNumStr(firstFourNum); 
			String secondFourStr = this.getFourNumStr(secondFourNum); 
			String lastFourStr = this.getFourNumStr(lastFourNum); 
			if (!lastFourStr.contains("仟")) 
				lastFourStr = "零" + lastFourStr; 
			if (!secondFourStr.contains("仟")) 
				secondFourStr = "零" + secondFourStr; 
			moneyStr = firstFourStr + "亿" + secondFourStr + "万" + lastFourStr 
					+ "元"; 
		} else 
			System.out.println("someting    is wrong"); 
		System.out.println(moneyStr); 
		return moneyStr; 
	} 
 
	public String getFourNumStr(int fourNum) { 
		int qian, bai, shi, ge; 
		ge = fourNum % 10; 
		shi = (fourNum % 100) / 10; 
		bai = (fourNum % 1000) / 100; 
		qian = fourNum / 1000; 
		String strTemp = ""; 
		if (qian != 0) { 
 
			strTemp = strTemp.concat(moneyStrs[qian] + "仟"); 
			strTemp = strTemp.concat(moneyStrs[bai] + "佰"); 
			strTemp = strTemp.concat(moneyStrs[shi] + "什"); 
			strTemp = strTemp.concat(moneyStrs[ge]); 
 
			strTemp = strTemp.replace("零佰", "零"); 
			strTemp = strTemp.replace("零什", "零"); 
			strTemp = strTemp.replace("零零", "零"); 
			strTemp = strTemp.replace("零零", "零"); 
 
			if (strTemp.endsWith("零")) 
				strTemp = strTemp.substring(0, strTemp.length() - 1); 
		} else if (bai != 0) { 
			strTemp = strTemp.concat(moneyStrs[bai] + "佰"); 
			strTemp = strTemp.concat(moneyStrs[shi] + "什"); 
			strTemp = strTemp.concat(moneyStrs[ge]); 
 
			strTemp = strTemp.replace("零什", "零"); 
			strTemp = strTemp.replace("零零", "零"); 
 
			if (strTemp.endsWith("零")) 
				strTemp = strTemp.substring(0, strTemp.length() - 1); 
		} else if (shi != 0) { 
 
			strTemp = strTemp.concat(moneyStrs[shi] + "什"); 
			strTemp = strTemp.concat(moneyStrs[ge]); 
 
			if (strTemp.endsWith("零")) 
				strTemp = strTemp.substring(0, strTemp.length() - 1); 
		} else 
			strTemp.concat(moneyStrs[ge]); 
		return strTemp; 
	} 
}

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/10114.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论