看疯狂Java讲义中的一个关于金钱转换的例题修改得来

来源:互联网 时间:1970-01-01

代码如下:

 

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 [email protected]

 * @version 1.0

 */

public class Num2Rmb

{

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

                   "伍" , "陆" , "柒" , "捌" , "玖"};

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

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

 

         /**

          * 把一个浮点数分解成整数部分和小数部分字符串

          * @param num 需要被分解的浮点数

          * @return 分解出来的整数部分和小数部分。第一个数组元素是整数部分,第二个数组元素是小数部分。

          */

         private String[] pide(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.pide(236711125.123)));

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

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

                  

                   //fing修改

                  

                   Scanner in = new Scanner(System.in);

                   double m = in.nextDouble();

                   String[] toM = nr.pide(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连续的时候无法实现正常输出。请各位给点儿意见。

相关阅读:
Top