人民币转换问题

/*

**写的比较凌乱,整体思路是四位为一体

*/

import java.text.DecimalFormat;
import java.util.Scanner;

public class ConvertMoney {
 public static void main(String[] args) {
  Scanner sc=new Scanner(System.in);
      double d=sc.nextDouble();  
  String s=conver(d);
  System.out.println(s);

 }

 private static String conver(double d) {
  
  String[]c={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
  String[]c2={"元","拾","佰","千"};
  String[]c3={"万","亿"};
  String[]c4={"角","分"};
  //System.out.println(c);
  String name;
     String temp;
  StringBuffer sb =  new StringBuffer();
  sb.append("人民币");
  DecimalFormat decimalFormat = new DecimalFormat("0.00");
      String str=decimalFormat.format(d);
     String st[]=str.split("\\.");
     // System.out.println(st[1]);
  int i=0;
  int len=st[0].length();
  int account = -1;
  if(len>=5&&len<9)account=0;
  if(len>=9&&len<12)account=1;
  //System.out.println(st[0]);
  if(len==1&&st[0].charAt(0)=='0'){
   name="";
   temp="";
  }else{
  for(i=0;i<len;i++){
   int j=len-i-1;
   int k=st[0].charAt(i)-'0';
   //第i个元素为0元,判断是不是最后元素以及判断i+1是否也为0
   if(st[0].charAt(i)=='0'){
     if(i==len-1){name="";temp=c2[j%4];}
     else if(st[0].charAt(i+1)=='0'){
      name="";
      temp="";
     }else{
         name=c[k];
         temp="";
     }
    }else{
     name=c[k];
     if(j%4==0&&j!=0){
      temp="";
     }
     else{
      temp=c2[j%4];
     }
     
    }
   sb.append(name);
   sb.append(temp);
   //System.out.println(sb);
   if(j%4==0){
    if(account!=-1){
     sb.append(c3[account]);
     account--;
    }
   }
     
   }
  }
  //处理小数部分
  int len1=st[1].length();
  int j=0;
  
  while(j<2){
   if(st[1].charAt(0)=='0'&&st[1].charAt(1)=='0'){
    sb.append("整");
    break ;
   }
   int k1=st[1].charAt(j)-'0';
   if(st[1].charAt(j)=='0'){
    j++;
   }else{
    name=c[k1];
    temp=c4[j];
    j++;
    sb.append(name);
    sb.append(temp);
   }
   
  }
  
   String s=sb.toString();
   return s; 
   }
  
 }


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值