金额数字转换到大写

package test;


public class TestSwing
{
    // 定义money中文
        private final static String[] pattern = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
        private final static String[] cPattern = { "", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿" };
        private final static String[] cfPattern = { "", "角", "分" };
        private final static String ZEOR = "零";
    public static void main(String[] args)
    {
        System.out.println("90807800.335");
        System.out.println(amtEnConvertCn("1666253.023"));
        System.out.println("900000000.335");
        System.out.println(amtEnConvertCn("900000000.335"));
        System.out.println("000000.0");
        System.out.println(amtEnConvertCn("00.0"));
        System.out.println(amtEnConvertCn("00.03"));
        System.out.println(amtEnConvertCn("00.30"));
        System.out.println(amtEnConvertCn("00.23"));
    }
    
    public static String amtEnConvertCn(String amt)
    {
        int dotPoint = amt.indexOf("."); // 判断是否为小数

        String moneyStr;

        if (dotPoint != -1)
        {
            moneyStr = amt.substring(0, amt.indexOf("."));
        }
        else
        {
            moneyStr = amt;
        }

        StringBuffer ms = new StringBuffer();                   // 整数部分的处理,以及最后的yuan.
        for (int i = 0; i < moneyStr.length(); i++)
        {
            ms.append(pattern[moneyStr.charAt(i) - 48]);        // 按数组的编号加入对应大写汉字
        }

        int cpCursor = 1;                                       // 添加位数字符
        for (int j = moneyStr.length() - 1; j > 0; j--)
        {
            ms.insert(j, cPattern[cpCursor]);                     // 在j之后加字符,不影响j对原字符串的相对位置
            cpCursor = cpCursor == 8 ? 1 : cpCursor + 1;        // 亿位之后重新循环
        }

        /* replace的起始于终止位置 */
        while (ms.indexOf("零拾") != -1)
        {
            // 当十位为零时用一个"零"代替"零拾"
            ms.replace(ms.indexOf("零拾"), ms.indexOf("零拾") + 2, ZEOR);
        }
        while (ms.indexOf("零佰") != -1)
        {
            // 当百位为零时,同理
            ms.replace(ms.indexOf("零佰"), ms.indexOf("零佰") + 2, ZEOR);
        }
        while (ms.indexOf("零仟") != -1)
        {
            ms.replace(ms.indexOf("零仟"), ms.indexOf("零仟") + 2, ZEOR);
        }
        while (ms.indexOf("零万") != -1)
        {
            // 万需保留,中文习惯
            ms.replace(ms.indexOf("零万"), ms.indexOf("零万") + 2, "万");
        }
        while (ms.indexOf("零亿") != -1)
        {
            ms.replace(ms.indexOf("零亿"), ms.indexOf("零亿") + 2, "亿");
        }
        while (ms.indexOf("零零") != -1)
        {
            // 有连续数位出现零,即有以下情况,此时根据习惯保留一个零即可
            ms.replace(ms.indexOf("零零"), ms.indexOf("零零") + 2, ZEOR);
        }
        while (ms.indexOf("亿万") != -1)
        {
            // 特殊情况,如:100000000,根据习惯保留高位
            ms.replace(ms.indexOf("亿万"), ms.indexOf("亿万") + 2, "亿");
        }
        while (ms.length() > 1 && ms.lastIndexOf("零") == ms.length() - 1)
        {
            // 当结尾为零j,不必显示,经过处理也只可能出现一个零 最后的一个零保留
            ms.delete(ms.lastIndexOf("零"), ms.lastIndexOf("零") + 1);
        }

        StringBuffer fraction = null;                             // 小数部分的处理,以及最后的yuan.
        int end;
        if ((dotPoint = amt.indexOf(".")) != -1)
        {
            // 是小数的进入
            String fs = amt.substring(dotPoint + 1, amt.length());

            if ("00".equals(fs) || "0".equals(fs))
            {
                fraction = new StringBuffer("元整");
            }
            else
            {
                // 若前两位小数全为零,则跳过操作
                end = fs.length() > 2 ? 2 : fs.length(); // 仅保留两位小数
                fraction = new StringBuffer(fs.substring(0, end));
                for (int j = 0; j < fraction.length(); j++)
                {
                    fraction.replace(j, j + 1, pattern[fraction.charAt(j) - 48]); // 替换大写汉字
                }
                for (int i = fraction.length(); i > 0; i--)
                { // 插入中文标识
                    fraction.insert(i, cfPattern[i]);
                }
                fraction.insert(0, "元"); // 为整数部分添加标识
            }
        }
        else
        {
            fraction = new StringBuffer("元整");
        }

        ms.append(fraction); // 加入小数部分
        return ms.toString();
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值