前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >金额转大写

金额转大写

作者头像
崔笑颜
发布2020-06-08 17:00:31
2K0
发布2020-06-08 17:00:31
举报

在处理财务账款时,需要将转账金额写成大写的。也就是说,如果要转账123456.00元,则需要写成“壹拾贰万叁仟肆佰伍拾陆元整”。 所以常常需要通过程序控制自动进行转换。本实例实现了小写金额到大写金额的转换。

具体代码如下:

代码语言:javascript
复制
import java.text.DecimalFormat;
import java.util.*;
//@北冥道人骑鲲打代码
public class f2 {
    private static String[] numBig = { "零", "壹", "贰", "叁", "肆",  "伍", "陆", "柒", "捌", "玖" };
    private static String[] numInt = { "", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟" };// 整数单位
    private static String[] numFloat = { "厘", "分", "角" };// 小数单位
    
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入金额");
        double money = input.nextDouble();

        //格式化double数字
        DecimalFormat df = new DecimalFormat("#0.###");//此时strNum小数位最多3位 
        String strNum = df.format(money);
        if (strNum.indexOf(".") > 0 ) {//判断是否有小数
            String strMoneyInt = strNum.substring(0, strNum.indexOf("."));
            if(strMoneyInt.length() > 12){
                System.out.println("数字太大了,转换不了");
            }else{
                System.out.println(getInt(strNum) + "元" + getDouble(strNum));
            }
        }
        else{
            if(strNum.length() > 12){
                System.out.println("数字太大了,转换不了");
            }else{
                System.out.println(getInt(strNum) + "元整");
            }
        }
    }
    
    //整数部分
    public static String getInt(String str) {
        if(str.indexOf(".") != -1){
            str = str.substring(0,str.indexOf("."));//截取小数点前面的数字
        }
        str = new StringBuffer(str).reverse().toString();//反转字符串
        StringBuffer strB = new StringBuffer(); //创建一个空的StringBuffer对象
        for (int i = 0; i < str.length(); i++){ //把单位添加进去
            strB.append(numInt[i]);
            strB.append(numBig[(str.charAt(i)-48)]);
            //str.charAt(i)-48,这里-48是因为str.charAt(i)-48为ASCII码
            //而参照ASCII码:
            //ASCII码为 48 ==》0
            //ASCII码为 49 ==》1 ...
        }
        str = strB.reverse().toString();//把反转过的字符串还原

        //替换字符串多于的字符
        if (str.indexOf("零拾") != -1){str = str.replace( "零拾", "零");}
        if (str.indexOf("零佰") != -1){str = str.replace( "零拾", "零");}
        if (str.indexOf("零仟") != -1){str = str.replace( "零拾", "零");}
        if (str.indexOf("零万") != -1){str = str.replace( "零拾", "万");}
        if (str.indexOf("零亿") != -1){str = str.replace( "零拾", "亿");}
        if (str.indexOf("零零") != -1){str = str.replace( "零拾", "零");}
        if (str.indexOf("亿万") != -1){str = str.replace( "零拾", "亿");}

        //除去零的结尾
        if (str.lastIndexOf("零") == str.length() - 1) {
            str = str.substring(0, str.length() - 1);
        }
        return str;
    }
    
    //小数部分
    public static String getDouble(String str) {
        str = str.substring(str.indexOf(".") + 1);//截取小数点后的数字

        //解决单位错位
        if(str.length() == 1){str = str +"00";}
        else if(str.length() == 2){str = str +"0";}

        str = new StringBuffer(str).reverse().toString();//反转字符串
        StringBuffer strB = new StringBuffer();//创建一个空的StringBuffer对象
        for (int i = 0; i < str.length(); i++) {//把单位添加进去
            strB.append(numFloat[i]);
            strB.append(numBig[str.charAt(i) - 48]);
        }
        str = strB.reverse().toString();//把反转过的字符串还原

        //替换字符串多于的字符
        if (str.indexOf("零角") != -1){str = str.replace( "零角", "零");}
        if (str.indexOf("零分") != -1){str = str.replace( "零分", "零");}
        if (str.indexOf("零厘") != -1){str = str.replace( "零厘", "零");}
        if (str.indexOf("零零") != -1){str = str.replace( "零零", "零");}

        //除去零的结尾
        if (str.lastIndexOf("零") == str.length() - 1) {
            str = str.substring(0, str.length() - 1);
        }
        return str;
    }
}
1966295-20200402101744624-131646338
1966295-20200402101744624-131646338
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-04-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档