A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© binglin 中级黑马   /  2015-8-17 22:51  /  493 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

金额转换,阿拉伯数字转换成中国传统形式。

例如:101000001010   转换为   壹仟零壹拾亿零壹仟零壹拾圆整

**@author Administrator
*/

public class Test10{  
    private static final char[] a = {'零','壹','贰','叄','肆','伍','陆','柒','捌','玖'};  
    private static final char[] b = {'仟','佰','拾',' ','仟','佰','拾',' ','仟','佰','拾','元',};  
  
    public static void convert(long money){  
               String s = String.valueOf( money);//将钱数转换为数字字符串
           char x[] = s.toCharArray();      //将数字字符串转换为字符数组
            char c[]=new char[x.length];
           
            int len = x.length;//字符串长度
               
                for(int j = 0; j < len; j++)
                {
                        int f=x[j]-'0';
                        c[j]=a[f];
                       
                }
               
                int r=0;//r判断是否写'零'。如:1001中第二个'零'就不写为“一千零一”
                int k=0;
                int m=0;//m判断是否写“万”。如:1000000000中只写为“一亿”,而110000000中写为“一亿一千万”
                /*
                 * 所给的钱数不低于“一亿”
                 */
               
                if(x.length>=9)
                {
                        for(;k<len-8;k++)
                        {
                                if(c[k]=='零'&&r==1)
                                        {
                                        if(k!=len-9)
                                            System.out.print(c[k]);
                                        r=0;
                                        }
                                else if(c[k]!='零')
                                {
                                      System.out.print(c[k]);
                                      System.out.print(b[b.length-len+k]);
                                      r=1;
                                }
                               
                        }
                        System.out.print('亿');
                        r=0;
               
                for(;k<len-4;k++)
                {
                        if(c[k]=='零'&&r==1)
                                {
                                if(k!=len-5)
                                System.out.print(c[k]);
                                r=0;
                                m=1;
                                }
                        else if(c[k]!='零')
                        {
                              System.out.print(c[k]);
                              System.out.print(b[b.length-len+k]);
                              r=1;
                              m=1;
                        }
                       
                }
                if(m==1)
                System.out.print('万');
                m=0;
                for(;k<len;k++)
                {
                        if(c[k]=='零'&&r==1)
                                {
                                if(k!=len-1)
                                System.out.print(c[k]);
                                r=0;
                                }
                        else if(c[k]!='零')
                        {
                              System.out.print(c[k]);
                              System.out.print(b[b.length-len+k]);
                              r=1;
                        }
                       
                }
               
                System.out.print('圆');
                if(c[len-1]=='零')
                        System.out.print('整');
    }  
               
                /*
                 * 所给的钱数不低于“一万”,但是低于“一亿”
                 */
                else if(c.length>=5&&c.length<9)
                {
                        for(;k<len-4;k++)
                        {
                                if(c[k]=='零'&&r==1)
                                        {
                                        if(k!=len-5)
                                        System.out.print(c[k]);
                                        r=0;
                                        }
                                else if(c[k]!='零')
                                {
                                      System.out.print(c[k]);
                                     
                                      System.out.print(b[b.length-len+k]);
                                      r=1;
                                }
                               
                        }
                        System.out.print('万');
                        for(;k<len;k++)
                        {
                                if(c[k]=='零'&&r==1)
                                        {
                                        if(k!=len-1)
                                        System.out.print(c[k]);
                                        r=0;
                                        }
                                else if(c[k]!='零')
                                {
                                      System.out.print(c[k]);
                                      System.out.print(b[b.length-len+k]);
                                      r=1;
                                }
                               
                        }
                       
                        System.out.print('圆');
                        if(c[len-1]=='零')
                                System.out.print('整');
                }
               
                /*
                 * 所给的钱数低于“一万”
                 */
               
                else
                {
                for(;k<len;k++)
                 {
                        if(c[k]=='零'&&r==1)
                                {
                                if(k!=len-1)
                                System.out.print(c[k]);
                                r=0;
                                }
                        else if(c[k]!='零')
                        {
                              System.out.print(c[k]);
                              System.out.print(b[b.length-len+k]);
                              r=1;
                        }
                       
                  }
                System.out.print('圆');
                if(c[len-1]=='零')
                        System.out.print('整');
                }
               
    }
   
      
    public static void main(String[] args){  
        Test10 rmb = new Test10();
       long money=101000001010L;
        convert(money); //调用金额转换转换类  
    }  
}

3 个回复

倒序浏览
代码好长,能化简不?
回复 使用道具 举报
风华正茂 来自手机 中级黑马 2015-8-18 12:32:34
藤椅
谢谢楼主分享,楼主辛苦了
回复 使用道具 举报
有用 学习了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马