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

uyu的头像 uyu 11 2014-12-31 23:05 0

 基本信息

× 1   

浏览数: 3695

分享时间: 4 年 前

2
/** 
 * @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;
	}
}


12 [下一页]

  • ossaa的头像 ossaa 2018-06-22 14:55 代码数:0

    Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place..

    בניית אתרים

  • wzg356的头像 wzg356 2015-01-03 18:13 代码数:0
    先收集,后学习
  • ossaa的头像 ossaa 2018-06-24 19:56 代码数:0

    I have read your blog it is very helpful for me. I want to say thanks to you. I have bookmark your site for future updates.

    Kredit Widerruf

  • ossaa的头像 ossaa 2018-06-25 20:38 代码数:0

    I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success in your business.

    coin dogs crypto game

  • gererik的头像 gererik 2018-07-04 15:03 代码数:0

    I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article.<a href='http://propertyshoppes.com/'>new condo launch</a>

  • gererik的头像 gererik 2018-07-07 13:04 代码数:0

    Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. Dentist 77375

  • gererik的头像 gererik 2018-07-09 17:27 代码数:0

    Nice post. I was checking constantly this blog and I am impressed! Extremely helpful information specially the last part   I care for such info a lot. I was seeking this particular information for a very long time. Thank you and good luck.  buy bulk youtube views

  • gererik的头像 gererik 2018-04-11 19:03 代码数:0

    Thanks for sharing nice information with us. i like your post and all you share with us is uptodate and quite informative, i would like to bookmark the page so i can come here again to read you, as you have done a wonderful job. small business bookkeeping

  • ossaa的头像 ossaa 2018-04-17 20:12 代码数:0

    Excellent information on your blog, thank you for taking the time to share with us. Amazing insight you have on this, it's nice to find a website that details so much information about different artists.

    acim

  • zeeman的头像 zeeman 2018-12-01 08:18 代码数:0

    I would like to thank you for this informative and interesting post. its interesting becuase I like it.

    find out here

您的评论: