当前位置: 移动技术网 > IT编程>开发语言>Java > java程序:转化金额

java程序:转化金额

2020年04月02日  | 移动技术网IT编程  | 我要评论

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

具体代码如下:

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;
    }
}

 

运行截图:

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网