Java小程序——将浮点数转换成人民币读法

自己在学习的过程中,根据书上部分知识,将此问题扩展而成;只是刚开始学习Java,编写的可能有些繁琐,还有divide的小功能没有实现。
import java.util.Arrays;
import java.io.*;
//将浮点数拆分时,11.034这类无法正确显示
public class Num2Rmb
{
    private String[] Num={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
    private String[] Unit={"","十","百","千"};
    private String[] Another=new String[]{"亿","万","元","角","分"};
    /*
    *@parameter 输入的一个数字
    *@return    返回将数字分成整数部分和小数部分
    */
    public String[] divide(double iptStr)
    {
        long zheng=(long)(iptStr);
        long xiao=Math.round((iptStr-zheng)*100);
        return new String[] {zheng+"",String.valueOf(xiao)};
    }

    /*将一个四位数转换成中文字符
    *@parameter String num
    *@return    读法
    */
    public String trans(String num,String num2)
    {
        String result="";
        int numLen=num.length();

        int zhengLen=numLen/4;
        int yuLen=numLen%4;
        int count=0;

        for(int i=0;i<numLen;i++)
        {
            int a=num.charAt(i)-48;
            if(i<yuLen)
            {
                if(a!=0&&(i+5-yuLen)%4!=0)
                {
                    result+=Num[a]+Unit[yuLen-1-i];
                }   
                else
                {
                    if(a==0)
                    {
                        result+=Num[a];
                    }
                    else
                    {
                        count++;
                        result+=Num[a]+Another[(5+count-zhengLen)%4];
                    } 
                }   
            }
            else
            {
                if(yuLen!=0)
                {
                    if(a!=0&&(i+5-yuLen)%4!=0)
                    {
                        result+=Num[a]+Unit[3-(i-yuLen)%4];
                    }   
                    else
                    {
                        if(a==0)
                        {
                            result+=Num[a];
                        }
                        else
                        {
                            count++;
                            result+=Num[a]+Another[(5+count-zhengLen)%4];
                        }
                    }   
                }
                else
                {
                    if(a!=0&&(i+5-yuLen)%4!=0)
                    {
                        result+=Num[a]+Unit[3-(i-yuLen)%4];
                    }   
                    else
                    {
                        if(a==0)
                        {
                            result+=Num[a];
                        }
                        else
                        {
                            count++;
                            result+=Num[a]+Another[(6+count-zhengLen)%4];
                        }
                    }   
                }
            }
        }
        for(int j=0;j<num2.length();j++)
        {
            int b=num2.charAt(j)-48;
            if(b!=0)
            {
                result+=Num[b]+Another[3+j];
            }
            else
            {
                result+=Num[b]; 
            }
        }           
        return result;  
    }
    public static void main(String[] args) throws Exception
    {   
        //必须在main函数那一行throws Exception才行
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String str=null;
        System.out.println("请输入一个你要转换的数字:");            
        str=br.readLine();
        double ip=Double.parseDouble(str);

        Num2Rmb test=new Num2Rmb();
        int strLen=test.divide(ip)[0].length();
        if(strLen>12||ip<10e-8)
        {
            System.out.println("ERROR!Please reinput a num within 12 position...(Not a negative number!)");
            return;
        }
        System.out.println(Arrays.toString(test.divide(ip)));
        System.out.println(test.trans(test.divide(ip)[0],test.divide(ip)[1]));
    }
}

“`

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值