用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

把数字转换成人民币大写的形式

2013-01-07 作者: 小蜜锋举报

[java]代码库

/** 
 * @author sunpander 
 * time: 08-12-11 19:16 
 * 把输入的数字转换成人民币大写的形式 
 */

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;
	}
}

[代码运行效果截图]


把数字转换成人民币大写的形式


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...