} } }

    看猖狂Java教材中的一个关于金钱转换的例题批改得来

    添加时间:2013-5-26 点击量:

    代码如下:



    import java.util.Arrays;


    import java.util.Scanner;


    /


      Description:


      <br/>网站: <a href=http://www.crazyit.org>猖狂Java联盟</a>


      <br/>Copyright (C), 2001-2012, Yeeku.H.Lee


      <br/>This program is protected by copyright laws.


      <br/>Program Name:


      <br/>Date:


      @author Yeeku.H.Lee kongyeeku@163.com


      @version 1.0


     /


    public class Num2Rmb


    {


             private String[] hanArr = {零 , 壹 , 贰 , 叁 , 肆 ,


                       伍 , 陆 , 柒 , 捌 , 玖};


             private String[] unitArr = {十 , 百 , 千};


             private String[] weiArr={,万,亿};



             /


               把一个浮点数分化成整数项目组和小数项目组字符串


               @param num 须要被分化的浮点数


               @return 分化出来的整数项目组和小数项目组。第一个数组元素是整数项目组,第二个数组元素是小数项目组。


              /


             private String[] divide(double num)


             {


                       //将一个浮点数强迫类型转换为long,即获得它的整数项目组


                       long zheng =  (long)num;


                       //浮点数减去整数项目组,获得小数项目组,小数项目组乘以100后再取整获得2位小数


                       long xiao = Math.round((num - zheng) 100);                  //Math.round(double d)返回最接近d的long,Math.round(float f)返回最接近f的int


                       //下面用了2种办法把整数转换为字符串


                       return new String[]{zheng + , String.valueOf(xiao)};             


             }



             /


               把一个四位的数字字符串变成汉字字符串


               @param numStr 须要被转换的四位的数字字符串


               @return 四位的数字字符串被转换成的汉字字符串。


              /


             private String toHanStr(String numStr)


             {


                       String result = ;


                       int numLen = numStr.length();


                       //依次遍历数字字符串的每一位数字


                       for (int i = 0 ; i < numLen ; i++ )


                       {


                                //把char型数字转换成的int型数字,因为它们的ASCII码值正好相差48


                                //是以把char型数字减去48获得int型数字,例如4被转换成4。


                                int num = numStr.charAt(i) - 48;


                                //若是不是最后一位数字,并且数字不是零,则须要添加单位(千、百、十)


                                if ( i != numLen - 1 && num != 0)


                                {


                                         result += hanArr[num] + unitArr[numLen - 2 - i];


                                }


                                //不然不要添加单位


                                else


                                {


                                         result += hanArr[num];


                                }


                       }


                       return result;


             }


            


             //fing批改


                       //将一个多余4位的字符串批改为多个四位的字符串


                       //最多支撑到12位的字符串


             private String[] fenJie(String numStr)


             {


                       int numLen = numStr.length();


                       //断定应当分化为几个四位字符串


                       int temp = (numLen+3)/4;


                       String[] s = new String[temp];


                       for (int i = 0;i<temp;i++)


                                s[i]=;


                       for ( int i = 0;i<numLen;i++)


                       {


                                s[(numLen-1-i)/4] += numStr.charAt(i);


                       }


                       return s;


             }


                       //小数项目组的金额


             private String toHan(String numStr)


             {       


                       String result = 元;


                       //把char型数字转换成的int


                       //是以把char型数字减去48获得int型数字,例如4被转换成4。


                       int num = numStr.charAt(0) - 0;


                       if (num != 0)


                                result += hanArr[num] + 角;


                       num = numStr.charAt(1) - 0;


                       if (num != 0)


                                result += hanArr[num] + 分;


                       return result;


             }


        public static void main(String[] args)


        {       


                       Num2Rmb nr = new Num2Rmb();


                       //测试把一个浮点数分化成整数项目组和小数项目组


                       System.out.println(Arrays.toString(nr.divide(236711125.123)));


                       //测试把一个四位的数字字符串变成汉字字符串


                       //System.out.println(nr.toHanStr(6109));


                      


                       //fing批改


                      


                       Scanner in = new Scanner(System.in);


                       double m = in.nextDouble();


                       String[] toM = nr.divide(m);


                       String[] ts = nr.fenJie(toM[0]);


                       String re = ;


                       System.out.println(ts.length);


                       for (int i = ts.length-1;i>=0;i--)


                       {


                                re += nr.toHanStr(ts[i]) + nr.weiArr[i];


                       }


                       System.out.println(re+nr.toHan(toM[1]));


                       in.close();


        }


    }



    本法度呈现的题目是呈现多个0连气儿的时辰无法实现正常输出。请各位给点儿定见。

    彼此相爱,却不要让爱成了束缚:不如让它成为涌动的大海,两岸乃是你们的灵魂。互斟满杯,却不要同饮一杯。相赠面包,却不要共食一个。一起歌舞欢喜,却依然各自独立,相互交心,却不是让对方收藏。因为唯有生命之手,方能收容你们的心。站在一起却不要过于靠近。—— 纪伯伦《先知》
    分享到: