java金额大写转换

注:金额只到9999,9999,9999,9999 千万亿级别

import java.util.HashMap;
import java.util.Map;

public class MoneyUtil
{
    public static Map<Integer,String> numMap = new HashMap<>();
    public static Map<Integer,String> unitMap = new HashMap<>();
    static {
        numMap.put(0,"");
        numMap.put(1,"");
        numMap.put(2,"");
        numMap.put(3,"");
        numMap.put(4,"");
        numMap.put(5,"");
        numMap.put(6,"");
        numMap.put(7,"");
        numMap.put(8,"");
        numMap.put(9,"");

        unitMap.put(2,"");
        unitMap.put(3,"");
        unitMap.put(4,"");
//        unitMap.put(5,"");
        unitMap.put(6,"");
        unitMap.put(7,"");
        unitMap.put(8,"");
//        unitMap.put(9,"亿");
        unitMap.put(10,"");
        unitMap.put(11,"");
        unitMap.put(12,"");
//        unitMap.put(13,"");
        unitMap.put(14,"");
        unitMap.put(15,"");
        unitMap.put(16,"");
    }

    /**
     * 将输入的金额转成人民币大写
     * @param momey
     * @return
     */
    public static String getUpperCaseMoney(long momey)
    {
        if (momey>0 && (momey+"").length()<=16)
        {
            String startNumStr = momey + "";
            StringBuilder outPut = new StringBuilder();
            char[] charArray = startNumStr.toCharArray();

            for (int i=startNumStr.length()-1,k=1,j=1; i>=0 ; i--,k++,j++)
            {
                Integer num = Integer.parseInt(charArray[i]+"");
                String numStr = numMap.get(num);
                String unit = unitMap.get(j);

                if (num==0)
                {
                    if(k==5)
                    {
                        outPut.insert(0,"");
                    }
                    else if (k==9)
                    {
                        if (outPut.charAt(0)=='')
                        {
                            outPut.replace(0,1,"");
                        }
                        outPut.insert(0,"亿");
                    }
                    else if (k==13)
                    {
                        outPut.insert(0,"");
                    }
                    if (outPut.length()>0)
                    {
                        if (outPut.charAt(0)!=''&& outPut.charAt(0)!='' && outPut.charAt(0)!='亿')
                        {
                            outPut.insert(0,'');
                        }
                    }
                }
                else
                {
                    if(k==5)
                    {
                        outPut.insert(0,"");
                    }
                    else if (k==9)
                    {
                        if (outPut.charAt(0)=='')
                        {
                            outPut.replace(0,1,"");
                        }
                        outPut.insert(0,"亿");
                    }
                    else if (k==13)
                    {
                        outPut.insert(0,"");
                    }
                    if (unit != null)
                        outPut.insert(0,numStr+unit);
                    else
                        outPut.insert(0,numStr);
                }
            }

            System.out.println(outPut.toString());
            return outPut.toString()+"圆整";
        }
        else
        {
            return "请输入长度小于16位的有效数字";
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值