/*
 * MoneyChange.java
 *
 * Created on 2007年10月1日, 下午2:59
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 *//**
 *程序目的:金额的中文大写形式           //10005.56这些情况没实现~请高手帮忙了~
 *形式:作业
 * @author luzhide
 */
public class MoneyChange {
    private String data[]={"","壹","贰","叁","肆","伍","陆","柒","捌","玖"}; 
    private String money_zheng[]={"亿","千","百","拾","万","千","百","拾",""};
    private String money_small[]={"角","分"};
    private String MoneyToZheng(int zheng)  //处理整数部分(算法不全,正在改进中)
    {
       String str_z1="";
       String str_z2="";
        String str_zheng="";
        int j=money_zheng.length-1;
        while(zheng>0&&j>0)
        {
            if(zheng%10==0)
            {
                str_zheng=""+str_zheng;
            }
            else
            {
                str_z1=data[zheng%10];
                str_z2=money_zheng[j];
                str_zheng=""+str_z1+str_z2+str_zheng;
            }
            zheng=zheng/10;
            j--;
        }
       
        return str_zheng+"元";
    }
    private String MoneyToSmall(int small)  //对小数部分进行转换
    {
        String str1="",str2="";
        String strSmall="";
        int i=money_small.length-1;
        if(small>=10)
        {
          while(small>0&&i>=0)
          {
              
                 if(small%10!=0)
                 {
                 str1=data[small%10];
                  str2=money_small;
                  strSmall=""+str1+str2+strSmall;
                 }
              
                  small=small/10;
                  i=i-1;
          }
        }
        else
        {
            if(small!=0)
            {
                strSmall+=data[small]+money_small[1];
            }
        }
        return strSmall;
    }
    private String  MoneyToChange(double number)
    {
        String strAll,str_int,str_sma,str_z;
        strAll="";str_int="";str_sma="";str_z="";
        int int_zheng,int_small;//
        //四舍五入 
        number=(number*100+0.5)/100; 
        
        int_zheng=(int)(number*100/100);//整数部分
        if(int_zheng!=0)
        {
        str_int=MoneyToZheng(int_zheng);
        }
        //小数部分 
        double temp=(number-int_zheng)*100*100/100; 
        //对小数部分四舍五入 
        int_small=(int)(temp*100+0.5)/100;
        if(int_small!=0)
        {
        str_sma=MoneyToSmall(int_small);
        }
        else
        {
            str_z="整";
        }
        strAll=str_int+str_sma+str_z;
        return strAll;
        
    }
    public static void main(String args[])
    {
        MoneyChange mon_change=new MoneyChange();
        double x=12340.00;
        System.out.println(mon_change.MoneyToChange(x));
    }
   
    
    
}
 

解决方案 »

  1.   

     
    修改下代码就可以了。
    while(small >0&&i >=0) 
              { 
                   
                     if(small%10!=0) 
                     { 
                     str1=data[small%10]; 
                      str2=String.valueOf(money_small); 
                      strSmall=""+str1+str2+strSmall; 
                     } 
                   
                      small=small/10; 
                      i=i-1; 
              } 
      

  2.   

    import java.text.DecimalFormat;
    import java.util.Scanner;public class ChineseCurrency {
        public static void main(String[] args) {        Scanner scan = new Scanner(System.in);
            System.out.println("please input the price:");
            double number = scan.nextDouble();        System.out.println(toChineseCurrency(new Double(number)));
        }    public static String toChineseCurrency(Object o) {
            if (o instanceof Number) {
                String s = new DecimalFormat("#.00").format(o);
                System.out.println(s);
                s = s.replaceAll("\\.", "");
                char[] digit = { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' };
                String unit = "仟佰拾兆仟佰拾亿仟佰拾万仟佰拾元角分";
                int l = unit.length();
                StringBuffer sb = new StringBuffer(unit);
                for (int i = s.length() - 1; i >= 0; i--)
                    sb = sb.insert(l - s.length() + i, digit[(s.charAt(i) - 0x30)]);
                s = sb.substring(l - s.length(), l + s.length());
                s = s.replaceAll("零[拾佰仟]", "零").replaceAll("零{2,}", "零")
                        .replaceAll("零([兆万元])", "$1").replaceAll("零[角分]", "");
                return s;
            } else {
                throw new NumberFormatException();
            }
        }
    }转自:http://blog.chinaunix.net/u/21684/showart.php?id=435241
      

  3.   

    这个类更完善,更符合实际应用的
    http://www.java2000.net/viewthread.jsp?tid=32&extra=page%3D1