java实现金额数字转换为中文大写

import java.io.*;
import java.lang.IllegalArgumentException;
public class ConvertNum{
	/**
     * 把金额阿拉伯数字转换为汉字表示,小数点后四舍五入保留两位
	 * 还有一种方法可以在转换的过程中不考虑连续0的情况,然后对最终的结果进行一次遍历合并连续的零
     */
	public static String [] ChineseNum = new String[]{"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
	public static String NumToChinese(double num){
	    if(num > 99999999999999.99 || num < -99999999999999.99)
            throw new IllegalArgumentException("参数值超出允许范围 (-99999999999999.99 ~ 99999999999999.99)!");
		boolean negative = false;//正负标号
		if(num<0){
			negative = true;
			num = num*(-1);
		}
		long temp = Math.round(num*100);
		int numFen=(int)(temp%10);//分
		temp=temp/10;
		int numJiao = (int)(temp%10);//角
		temp=temp/10;
		//此时temp只包含整数部分
		int [] parts =new int[20];//将金额整数部分分为在0-9999之间数的各个部分
		int numParts = 0;//记录把原来金额整数部分分割为几个部分 
		for(int i=0;;i++){
			if(temp == 0)
				break;
			int part = (int)(temp%10000);
			parts[i] =part;
			temp = temp/10000;
			numParts++;
		}
		boolean beforeWanIsZero = true;//标志位,记录万的下一级是否为0
		String chineseStr = "";
		for(int i=0;i0)//如果万的部分不为0,而万前面的部分小于1000大于0,则万后面应该跟零
							chineseStr = "零"+chineseStr;
						chineseStr = "万"+chineseStr;
					}
				}
			}
			chineseStr = partChinese + chineseStr;
		}
		if("".equals(chineseStr))//整数部分为0,则表示为零元
			chineseStr = ChineseNum[0];
		else if(negative)//整数部分部位0,但是为负数
			chineseStr = "负" +chineseStr;
		chineseStr = chineseStr + "元";
		if(numFen==0&&numJiao==0){
			chineseStr = chineseStr +"整";
		}
		else if(numFen==0){//0分
			chineseStr = chineseStr +ChineseNum[numJiao] + "角";
		}
		else{
			if(numJiao==0)
				chineseStr = chineseStr + "零" + ChineseNum[numFen] + "分";
            else
                chineseStr = chineseStr + ChineseNum[numJiao] + "角" + ChineseNum[numFen] + "分";
		}
		return chineseStr;
	}
	//转换拆分后的每个部分,0-9999之间
	public static String partConvert(int partNum){
		if(partNum<0||partNum>10000){
			throw new IllegalArgumentException("参数必须是大于等于0或小于10000的整数");
		}
		String [] units = new String[]{"","拾","佰","仟"};
		int temp = partNum;
		String partResult = new Integer(partNum).toString();
		int partResultLength = partResult.length();
		boolean lastIsZero = true;//记录上一位是否为0
		String chineseStr = "";
		for(int i=0;i

你可能感兴趣的:(面试题)