`
qq1988627
  • 浏览: 103376 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

数字转中文

    博客分类:
  • Java
 
阅读更多
package zxc.utils;

import java.util.HashMap;
import java.util.Map;

public class RmbConversion {

	static StringBuffer	buf;
	
	public static void main(String[] args) {
		System.out.println(insensitive(1000));
	}

	/** *主方法(负责完成小写转大写)* */
	public static StringBuffer insensitive(double totalMoney) {
		int count = 0;
		buf = new StringBuffer();
		String[] digit = { "分", "角", "元", "拾", "佰", "仟", "万" };
		int shang, mo = 0;
		int totalMoneys = (int) (totalMoney * 100);

		//int totalMoneys = 5523;
		/** *这个循环的大小其实可以根据digit数组的长度来控制** */
		Integer integer = 10000000;
		for (int exponent = digit.length - 1; exponent >= 0; exponent--) {
			integer /= 10;
			shang = totalMoneys / integer;
			mo = totalMoneys % integer;
			if(count == 0){
				count = getMarker(shang); //设置的标记数,用来判断'0'出现的情况(有效部分的'0',无效部分的'0')
			}		
			setValue(shang, digit[exponent],count, mo);
			totalMoneys = mo;
		}
		return buf;
	}

	/***对传递过来的整数进行赋值***/
	public static void setValue(int zheShu, String digit, int count, int mo) {
		if (zheShu > 0) {
			buf.append(getConstant(zheShu) + digit);
		} else if(zheShu == 0){
			if(count > 0 && mo >0)
			{	//有效'0'
				buf.append(getConstant(zheShu) + digit);
			}else{
				//无效'0'
				buf.append("×" + digit);
			}	
		}
	}
	
	/*对传递进来的数进行判断,如果大于0则加一*/
	public static int getMarker(int number){
		if(number > 0){
			return 1;
		} 
		return 0;
	}

	public static String getConstant(int constant) {
		Map<Integer, String> num = new HashMap<Integer, String>();
		String[] number = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
		for (int i = 0; i < 10; i++) {
			num.put(i, number[i]);
		}
		return num.get(constant).toString();
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics