数组的应用——将浮点数转为人民币读法

package start_001;

import java.util.Arrays;

public class changeMoney {
    //金钱的个位数读法数组
    private String[] moneyNum={"","","","","","","","",""};
    private String[] moneyWeight={"","",""};
    //将浮点数分为整数和分数,整数和分数组成数组
    public String[] divide(double money)
    {
        long zheng=(long)money;
        //获得小数部分的前两位;
        int xiao=(int)Math.round((money-zheng)*100);
        return new String[]{zheng+"",String.valueOf(xiao)};
    }
    /*
     * 把一个四位数的数字转为汉子字符串
     */
    public String intChangeToWord(String num)
    {
        String result="";
        int numLength=num.length();
        //依次遍历数字字符串的每一位数字
        for(int i=0;i<numLength;i++)
        {
            //将char型数字换位int型
            int numChar=num.charAt(i)-48;
            //如果不是最后一位数字而且数字不为零,需要加单位
            if(i!=numLength-1&&numChar!=0)
            {
                result+=moneyNum[numChar-1]+moneyWeight[numLength-2-i];
                
            }
            else
            {
                result+=moneyNum[numChar];
            }
        }
        return result;
    }
public static void main(String args[])
{
    changeMoney change=new changeMoney();
System.out.println(Arrays.toString(change.divide(21212121.21212)));    
System.out.println(change.intChangeToWord("1234"));
}
}

 

 

posted @ 2017-02-21 18:49  叮咚叮咚126  阅读(388)  评论(0编辑  收藏  举报