数字金额转换为中文金额_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 数字金额转换为中文金额

数字金额转换为中文金额

 2014/11/17 13:06:48  magic_agate  程序员俱乐部  我要评论(0)
  • 摘要:/*文档密级:秘密*/packagecom.str;importjava.math.BigDecimal;publicclassChangeRMB{StringstrCash;intDotPos;publicChangeRMB(){}publicvoidsetCash(Stringcash){this.initRMB(cash);}publicChangeRMB(StringCash){this.initRMB(Cash);}privatevoidinitRMB(StringCash){if
  • 标签:
class="java" name="code">
/* 文档密级:秘密  */
package com.str;

import java.math.BigDecimal;


public class ChangeRMB{
	String strCash;
	int DotPos;
    
    public ChangeRMB(){
    }
    
    public void setCash(String cash){
        this.initRMB(cash);
    }
    
	public ChangeRMB(String Cash){
		this.initRMB(Cash);
	}

	private void initRMB(String Cash){
		if(Cash.equals("")){
			Cash = "0";
		}
		strCash = Cash;
		while(strCash.indexOf(",") >= 0){
			strCash = strCash.substring(0,strCash.indexOf(",")) + strCash.substring(strCash.indexOf(",") + 1,strCash.length());
		}
		strCash = (new BigDecimal(strCash)) + "";
		//得到小数点位置
		DotPos = strCash.indexOf("."); //返回小数点的开始位置
		if(DotPos == -1){
			DotPos = strCash.length(); //判断是否为整数
			//只取小数点后二位
		}
		if(DotPos < strCash.length() - 3){
			strCash = strCash.substring(0,strCash.indexOf(".") + 3);
		}
		if(DotPos == strCash.length() - 2){
			strCash = strCash + "0";
		}
	}

	public String getChnRMB(){
		String strRet = ""; //输出大写字符串变量
		boolean bFlag = true; //改变对'零'的判断条件变量
		boolean zFlag = true;
		int i;
		if(Double.parseDouble(strCash) == 0){
			return "零";
		}
		//先取小数点后的数
		for(i = DotPos + 1;i < strCash.length();i++){
			if(Integer.parseInt(strCash.substring(i,i + 1)) != 0){
				strRet = strRet + GetNumber(Integer.parseInt(strCash.substring(i,i + 1))) + GetUnit(DotPos - i);
				zFlag = false;
			}

		}

		//最后位不为'角,分'
		if(DotPos == -1 || zFlag || DotPos == strCash.length() - 1){
			strRet += "整";
		}
		//取小数点前的数,i用作判断位置
		for(i = DotPos - 1;i >= 0;i--){
			if(Integer.parseInt(strCash.substring(i,i + 1)) != 0){
				strRet = GetNumber(Integer.parseInt(strCash.substring(i,i + 1))) + GetUnit(DotPos - i) + strRet;
				bFlag = false;
			}
			else{
				if(!bFlag){
					strRet = "零" + strRet;
				}
				bFlag = true;
				if(DotPos > 8){
                                    //modified by NT 2003-02-26
                                    //if((DotPos - i == 5 && !strCash.substring(i - 4,i + 1).equals("00000")) ||
                                    if((DotPos - i == 5 && !strCash.substring(i,DotPos).equals("00000")) ||

					   (DotPos - i == 9) || (DotPos - i == 13)){
						strRet = GetUnit(DotPos - i) + strRet;
					}
				}
				else{
					if((DotPos - i == 5) || (DotPos - i == 9) || (DotPos - i == 13)){
						strRet = GetUnit(DotPos - i) + strRet;
					}
				}

				if(DotPos - i == 1){
					strRet = "元" + strRet;
				}
			}
		}
		if(strRet.indexOf("元") == 0){
			strRet = strRet.substring(1);
		}
		return strRet;
	}

	public String GetNumber(int nNum){
		String strRet = "";
		switch(nNum){
		case 0:
			strRet = "零";
			break;
		case 1:
			strRet = "壹";
			break;
		case 2:
			strRet = "贰";
			break;
		case 3:
			strRet = "叁";
			break;
		case 4:
			strRet = "肆";
			break;
		case 5:
			strRet = "伍";
			break;
		case 6:
			strRet = "陆";
			break;
		case 7:
			strRet = "柒";
			break;
		case 8:
			strRet = "捌";
			break;
		case 9:
			strRet = "玖";
			break;
		}
		return strRet;
	}

	private static String GetUnit(int nUnit){
		String strRet = "";
		switch(nUnit){
		case -2:
			strRet = "分";
			break;
		case -1:
			strRet = "角";
			break;
		case 1:
			strRet = "元";
			break;
		case 2:
			strRet = "拾";
			break;
		case 3:
			strRet = "佰";
			break;
		case 4:
			strRet = "仟";
			break;
		case 5:
			strRet = "万";
			break;
		case 6:
			strRet = "拾";
			break;
		case 7:
			strRet = "佰";
			break;
		case 8:
			strRet = "仟";
			break;
		case 9:
			strRet = "亿";
			break;
		case 10:
			strRet = "拾";
			break;
		case 11:
			strRet = "佰";
			break;
		case 12:
			strRet = "仟";
			break;
		case 13:
			strRet = "万";
			break;
		case 14:
			strRet = "拾";
			break;
		case 15:
			strRet = "佰";
			break;
		case 16:
			strRet = "仟";
			break;
		case 17:
			strRet = "万";
			break;

		}
		return strRet;
	}

	public String getEngRMB(){
		String strRet = ""; //输出大写字符串变量
		// boolean bFlag = true; //改变对'零'的判断条件变量
		boolean zFlag = true;
		int i;
		i = DotPos + 1;

		if(Double.parseDouble(strCash) == 0){
			return null;
		}

		//先取小数点后的数
		if(DotPos != strCash.length()){
			if(Integer.parseInt(strCash.substring(i)) >= 20){
				strRet = strRet + GetEngTwoNumber(Integer.parseInt(strCash.substring(i,i + 1))) + GetEngNumber(Integer.parseInt(strCash.substring(i + 1)));
				zFlag = false;
			}
			if(Integer.parseInt(strCash.substring(i)) < 20){
				if(Integer.parseInt(strCash.substring(i)) > 10){
					strRet = strRet + GetEngOneNumber(Integer.parseInt(strCash.substring(i + 1,i + 2)));
					zFlag = false;
				}
				else{
					if(Integer.parseInt(strCash.substring(i)) > 0){
						strRet = strRet + GetEngNumber(Integer.parseInt(strCash.substring(i + 1,i + 2)));
						zFlag = false;
					}
				}
			}
		}
		//最后位不为'角,分'
		if(DotPos == -1 || zFlag){
			strRet += " ONLY";
		}
		else{
			strRet = " POINT" + strRet;
		}
		//取小数点前的数,i用作判断位置
		for(i = DotPos - 1;i >= 0;i--){
			if(DotPos - i == 1 || DotPos - i == 4 || DotPos - i == 7 || DotPos - i == 10){
				if(i > 0){
					if(Integer.parseInt(strCash.substring(i - 1,i + 1)) >= 20){
						strRet = GetEngTwoNumber(Integer.parseInt(strCash.substring(i - 1,i))) + GetEngNumber(Integer.parseInt(strCash.substring(i,i + 1))) + GetEngUnit(DotPos - i) +
								 strRet;
					}
					if(Integer.parseInt(strCash.substring(i - 1,i + 1)) < 20){
						if(Integer.parseInt(strCash.substring(i - 1,i + 1)) > 10){
							strRet = GetEngOneNumber(Integer.parseInt(strCash.substring(i,i + 1))) + GetEngUnit(DotPos - i) + strRet;
						}
						else{
							if(Integer.parseInt(strCash.substring(i - 1,i + 1)) > 0){
								strRet = GetEngNumber(Integer.parseInt(strCash.substring(i,i + 1))) + GetEngUnit(DotPos - i) + strRet;
							}
							if(DotPos - i == 7 && Integer.parseInt(strCash.substring(i - 1,i + 1)) == 0){
								strRet = GetEngUnit(DotPos - i) + strRet;
							}

							if(DotPos - i == 4 && DotPos < 7 && Integer.parseInt(strCash.substring(i - 1,i + 1)) == 0){
								strRet = GetEngUnit(DotPos - i) + strRet;
							}
						}
					}
				}
				else{
					strRet = GetEngNumber(Integer.parseInt(strCash.substring(i,i + 1))) + GetEngUnit(DotPos - i) + strRet;
				}
			}

			if((DotPos - i == 3) || (DotPos - i == 6) || (DotPos - i == 9)){
				if(Integer.parseInt(strCash.substring(i,i + 1)) != 0){
					strRet = GetEngNumber(Integer.parseInt(strCash.substring(i,i + 1))) + GetEngUnit(DotPos - i) + strRet;
				}
			}

		}
		return strRet;
	}

	private String GetEngNumber(int nNum){
		String strRet = "";
		switch(nNum){
		case 0:
			strRet = " TEN";
			break;
		case 1:
			strRet = " ONE";
			break;
		case 2:
			strRet = " TWO";
			break;
		case 3:
			strRet = " THREE";
			break;
		case 4:
			strRet = " FOUR";
			break;
		case 5:
			strRet = " FIVE";
			break;
		case 6:
			strRet = " SIX";
			break;
		case 7:
			strRet = " SEVEN";
			break;
		case 8:
			strRet = " EGIHT";
			break;
		case 9:
			strRet = " NINE";
			break;

		}
		return strRet;
	}

	private String GetEngOneNumber(int nNum){
		String strRet = "";
		switch(nNum){
		case 1:
			strRet = " ELEVEN ";
			break;
		case 2:
			strRet = " TWELVE ";
			break;
		case 3:
			strRet = " THREETEEN ";
			break;
		case 4:
			strRet = " FOURTEEN ";
			break;
		case 5:
			strRet = " FIVTEEN ";
			break;
		case 6:
			strRet = " SIXTEEN ";
			break;
		case 7:
			strRet = " SEVENTEEN ";
			break;
		case 8:
			strRet = " EGIHTEEN ";
			break;
		case 9:
			strRet = " NINTEEN ";
			break;

		}
		return strRet;
	}

	private String GetEngTwoNumber(int nNum){
		String strRet = "";
		switch(nNum){
		case 2:
			strRet = " TWEENTY";
			break;
		case 3:
			strRet = " THIRTY";
			break;
		case 4:
			strRet = " FOURTY";
			break;
		case 5:
			strRet = " FIVTY";
			break;
		case 6:
			strRet = " SIXTY";
			break;
		case 7:
			strRet = " SEVENTY";
			break;
		case 8:
			strRet = " EGIHTY";
			break;
		case 9:
			strRet = " NINTY";
			break;
		}
		return strRet;
	}

	private static String GetEngUnit(int nUnit){
		String strRet = "";
		switch(nUnit){
		case 3:
			strRet = " HUNDRED";
			break;
		case 4:
			strRet = " THOUSAND";
			break;
		case 6:
			strRet = " HUNDRED";
			break;
		case 7:
			strRet = " MIllION";
			break;
		case 9:
			strRet = " HUNDRED";
			break;
		case 10:
			strRet = " BILLION";
			break;
		}
		return strRet;
	}

	public static void main(String[] args){
		ChangeRMB ChangeRMB1 = new ChangeRMB("103050020");
		System.out.println(ChangeRMB1.getChnRMB());
	}
}
  • 相关文章
发表评论
用户名: 匿名