Java实现两位小数的数字转换为中文金额

两位小数的数字转换成中文金额

问题描述:设计一个函数,完成把包含两位小数的数字转换成中文金额,如输入 “1034.50”,输出为 “⊗ 壹仟零叁拾肆元伍角整” 或 “人民币壹仟零叁拾肆元伍角整”。

原始代码如下:

package com.lyp.test;

import java.util.Scanner;

public class money {
    private static String[] num1 = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
    private static String[] num2 = {"元","角","分","零", "拾", "佰", "仟", "万", "亿"};

    public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入金额:");//必须小于2147483648,int最大值2147483647,默认读取两位小数
        String s = sc.nextLine();
            //money = Float.valueOf(s).floatValue();
            double money1 = Double.parseDouble(s);
            double m = (money1 % 1) * 100;
            int money2 = (int)money1;
            int temp = (int)m;
            String res = translate(money2,temp);
            System.out.println("人民币"+res+"整");
            sc.close();
    }



    //设计一个函数,完成把包含两位小数的数字转换成中文金额,如输入 “1034.50”,
    //输出为 “⊗ 壹仟零叁拾肆元伍角整” 或 “人民币壹仟零叁拾肆元伍角整”。

    /**
     * 将数字转换为大写
     * @param money
     */
    public static String translate (int money,int temp){
        if(money < 0){
            return "";
        }
        if(money == 0){
            return num1[0];      }
        int w = 10000;
        int n1 = money / (w * w);
        int n2 = (money / w) % w;
        int n3 = money % w;
        int n4 = temp / 10;
        int n5 = temp % 10;
        StringBuffer sb = new StringBuffer();
        //亿
        if(n1 > 0){
            sb.append(translate1000(n1));
            sb.append(num2[8]);
        }
        //万
        if(n2 > 0){
            sb.append(translate1000(n2));
            sb.append(num2[7]);
        } else {
            if (n1 != 0){
                sb.append(num1[0]);
            }
        }
        //万以下
        if(n3 > 0){
            sb.append(translate1000(n3));
        } else {
            if (n2 != 0){
                sb.append(num1[0]);
            }
        }
        sb.append(num2[0]);
        //角
        if(n4 > 0) {
            sb.append(num1[n4]);
            sb.append((num2[1]));
        }
        //分
        if(n5 > 0){
            sb.append(num1[n5]);
            sb.append((num2[2]));
        }
        return sb.toString();
    }

    /**
     * 将千元以下转换大写
     * @param money
     */
    public static String translate1000(int money){
        if(money > 9999 || money < 0){
            return "";
        }
        int x1 = money / 1000;
        int x2 = (money / 100) % 10;
        int x3 = (money / 10) % 10;
        int x4 = money % 10;

        StringBuffer sb = new StringBuffer();
        //千
        if(x1 > 0){
            sb.append(num1[x1]);
            sb.append(num2[(6)]);
        }
        //百
        if(x2 > 0){
            sb.append(num1[x2]);
            sb.append(num2[5]);
        } else {
            if(x1 != 0){
                sb.append(num1[0]);
            }
        }
        //十
        if(x3 > 0){
            sb.append(num1[x3]);
            sb.append(num2[4]);
        } else {
            if(x2 != 0){
                sb.append(num1[0]);
            }
        }
        //个
        if(x4 > 0){
            sb.append(num1[x4]);
        }
        return sb.toString();
    }
}

优化后代码如下:

①增加了对非法字符的判断
②由int 改为 long增加了可以识别的有效位数
③优化了对于各个位为零的判定
④修复了double转换long(int)类型的精度丢失问题

package com.lyp.test;

import java.math.BigDecimal;
import java.util.Scanner;

public class money {
    private static String[] num1 = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
    private static String[] num2 = {"元","角","分","零", "拾", "佰", "仟", "万", "亿"};
    private static int flag = 1;

    public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入金额:");
            String s = sc.nextLine();
            //double money1 = Float.valueOf(s).floatValue();
            boolean isNum = isNumeric(s);
            if(isNum){
                double money1 = Double.parseDouble(s);
                double m = (money1 % 1) * 100;
                long money2 = formatDoubleTOLong(money1,1.0);
                long temp = formatDoubleTOLong(m,1.0);
                String res = translate(money2,temp);
                if(flag == 1){
                    System.out.println("人民币"+res+"整");
                }
            } else {
                System.out.println("输入的金额无效!!!");
            }
            sc.close();
    }



    //设计一个函数,完成把包含两位小数的数字转换成中文金额,如输入 “1034.50”,
    //输出为 “⊗ 壹仟零叁拾肆元伍角整” 或 “人民币壹仟零叁拾肆元伍角整”。

    /**
     * 将数字转换为大写
     * @param money
     * @param temp
     * @return
     */
    public static String translate (long money,long temp){
        if(money < 0 || temp < 0){
            flag = 0;
            System.out.println("金额不能为负!!!");
        }
        if(money == 0 && temp == 0){
            flag = 0;
            System.out.println("金额为零!!!");
        }
        int w = 10000;
        int n1 = (int)(money / (w * w));
        int n2 = (int)((money / w) % w);
        int n3 = (int)(money % w);
        int n4 = (int)(temp / 10);
        int n5 = (int)(temp % 10);
        StringBuffer sb = new StringBuffer();
        //亿
        if(n1 > 0 && n1 <9999){
            sb.append(translate1000(n1));
            sb.append(num2[8]);
        } else if(n1 > 9999){
            flag = 0;
            System.out.println("金额超出范围!!!");
        }
        //万
        if(n2 > 999){
            sb.append(translate1000(n2));
            sb.append(num2[7]);
        }else {
            if(n2 > 0){
                if (n1 != 0){
                    sb.append(num1[0]);
                }
                sb.append(translate1000(n2));
                sb.append(num2[7]);
            }
        }
        //万以下
        if(n3 > 0){
            if(n2 != 0){
                if(n3 > 999){
                    sb.append(translate1000(n3));
                } else {
                    sb.append(num1[0]);
                    sb.append(translate1000(n3));
                }
            } else {
                if(n1 != 0){
                    sb.append(num1[0]);
                }
                sb.append(translate1000(n3));
            }
        }
        if(n1 != 0 || n2 !=0 || n3 != 0){//当整数部分不为空时
            sb.append(num2[0]);
        }
        //角
        if(n4 > 0) {
            sb.append(num1[n4]);
            sb.append((num2[1]));
        } else if (n5 != 0){
            sb.append(num1[0]);
        }
        //分
        if(n5 > 0){
            sb.append(num1[n5]);
            sb.append((num2[2]));
        }
        return sb.toString();
    }

    /**
     * 将千元以下转换大写
     * @param money
     */
    public static String translate1000(long money){
        if(money > 9999 || money < 0){
            return "";
        }
        int x1 = (int)(money / 1000);
        int x2 = (int)((money / 100) % 10);
        int x3 = (int)((money / 10) % 10);
        int x4 = (int)(money % 10);

        StringBuffer sb = new StringBuffer();
        //千
        if(x1 > 0){
            sb.append(num1[x1]);
            sb.append(num2[(6)]);
        }
        //百
        if(x2 > 0){
            sb.append(num1[x2]);
            sb.append(num2[5]);
        } else {
            if(x1 != 0 && x3 != 0){//当千位和十位都不为零时,百位为零时才添上一个零
                sb.append(num1[0]);
            }
        }
        //十
        if(x3 > 0){
            sb.append(num1[x3]);
            sb.append(num2[4]);
        } else {
            if(x2 != 0 && x4 != 0){//当百位和个位都不为零时,十位为零时才添上一个零
                sb.append(num1[0]);
            }
        }
        //个
        if(x4 > 0){
            sb.append(num1[x4]);
        }
        return sb.toString();
    }
    //解决 强转精度丢失问题
    public static long formatDoubleTOLong(double dou1,double dou2){
        BigDecimal big1 = new BigDecimal(Double.valueOf(dou1)).setScale(2, BigDecimal.ROUND_HALF_UP);
    BigDecimal big2 = new BigDecimal(Double.valueOf(dou2));
    return big1.multiply(big2).longValue();
 }
    //判断输入的是否为浮点数或整数
    public static boolean isNumeric(String str){
        int point = 0;
        for(int i=str.length();--i>=0;){
            int chr=str.charAt(i);
            if(chr == 46)
            {
                point++;
            }
            if(point > 1){
                return false;
            }
            if((chr<48 || chr>57) && (chr != 46))//排除掉符号点
            {
                return false;
            }
    }
        return true;
    }


}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值