浮点数转换成人民币读法字符串

自己今天写了一个疯狂java上demo——double转人民币大写<pre name="code" class="java">
import java.util.Scanner;


public class num2Rmb {
	String [] hanrrr = { "零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
	String [] unitrrr = {"拾","佰","仟"} ;
	double num  ;
	
	public num2Rmb (double num) {
		this.num = num ;
	}
	public String []divide(){
		long zheng = (long)num ;
		long xiao = Math.round((num - zheng) *100) ;
		return new String[]{zheng+"",String.valueOf(xiao)} ;
	}
	public String getZheng(String numStr) {
		
		boolean flag = true ;
		String result = "" ;
		int index = 0;
		//判断第一位是不是0;
		while(numStr.charAt(index)=='0'){
			index++ ;
		}
		int numLength = numStr.length() ;
		
		for (int i = index; i < numStr.length()-1; i++) {
			int num = numStr.charAt(i) -48;
			int pos = numLength - i -2 ;
			int tempUnit = pos % 4 ;
			if (num!= 0 ) {
				flag =false ;
				if (tempUnit !=3) {
					result +=hanrrr[num]+unitrrr[tempUnit] ; 
					
				}else {
					result +=hanrrr[num] ; 
					if(pos / 4 !=1){
						result += "万" ;
					}else {
						result +="亿" ;
					}
				}
			}else if(tempUnit !=3){
				if (flag==false) {
					result += "零";
				}
			}else {
				if(pos / 4 !=2){
					result += "万" ;
				}else {
					result +="亿" ;
				}	
			}
			
		}	
		int last = numStr.charAt(numLength - 1)-48 ;
		return result+hanrrr[last];
		
	}
	public String getXiao(String numStr){
		if (numStr.length() == 0) {
			return "" ;
		}
		if (numStr.length() ==1) {
			numStr += 0 ;
		}
		
		int frist = numStr.charAt(0) - 48;
		int second = numStr.charAt(1) - 48;
		if (frist != 0 && second != 0) {
			return hanrrr[frist]+"角"+hanrrr[second]+"分" ; 
		}else if (frist != 0 ) {
			return hanrrr[frist]+"角" ;
		}else if(second != 0) {
			return hanrrr[second]+"分" ;
		}else {
			return "" ;
		}
	}
	public String getEntity(){

		String [] parts = divide();
		String zheng = getZheng(parts[0]);
		String xiao = getXiao(parts[1]) ;
		return zheng+xiao ;
	}
	public static void main(String[] rgs) {
		System.out.print("请输入您的金额:");
		Scanner sc  = new Scanner(System.in);
		double num = sc.nextDouble() ;
		num2Rmb temp = new num2Rmb(num);
		sc.close();
		System.out.println();
		System.out.print("输出:");
		System.out.println(temp.getEntity());
	}

}


                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值