浮点数转为人民币金额大写

感谢Trumplet共享代码
http://community.csdn.net/Expert/TopicView3.asp?id=4858189


import java.text.DecimalFormat;

/**
 * 浮点数转为人民币金额大写
 * @author Trumplet
 */
public class NumberToZH
{
    static final String zhnum_0 = "零壹贰叁肆伍陆柒捌玖";
    static final String zhnum = "零一二三四五六七八九";
    static final String[] zhnum1 = {"", "十", "百", "千"};
    static final String[] zhnum1_0 = {"", "拾", "佰", "仟"};
    static final String[] zhnum2 = {"", "万", "亿", "万亿", "亿亿"};
                               
    /** Creates a new instance of NumberToZH */
    public NumberToZH()
    {
    }

    private static String numberToZH4(String s, boolean fan)
    {
        StringBuffer sb = new StringBuffer();
        if (s.length() != 4)
            return null;
        for (int i = 0; i < 4; i++)
        {
            char c1 = s.charAt(i);
            if (c1 == '0' && i > 1 && s.charAt(i - 1) == '0')
                continue;
            if (c1 != '0' && i > 1 && s.charAt(i - 1) == '0')
                sb.append('零');
            if (c1 != '0')
            {
                if (fan)
                {
                    sb.append(zhnum_0.charAt(c1 - 48));
                    sb.append(zhnum1_0[4 - i - 1]);
                }
                else
                {
                    sb.append(zhnum.charAt(c1 - 48));
                    sb.append(zhnum1[4 - i - 1]);
                }
            }
        }
        return new String(sb);
    }

    public static String numberToZH(long n, boolean fan)
    {
        StringBuffer sb = new StringBuffer();
        String strN = "000" + n;
        int strN_L = strN.length() / 4;
        strN = strN.substring(strN.length() - strN_L * 4);
        for (int i = 0; i < strN_L; i++)
        {
            String s1 = strN.substring(i * 4, i * 4 + 4);
            String s2 = numberToZH4(s1, fan);
            sb.append(s2);
            if (s2.length() != 0)
                sb.append(zhnum2[strN_L - i - 1]);
        }
        String s = new String(sb);
        if (s.length() != 0 && s.startsWith("零"))
            s = s.substring(1);
        return s;
    }

    public static String numberToZH(double d, boolean fan)
    {
        return numberToZH("" + d, fan);
    }

    public static String numberToZH(String str, boolean fan)
    {
        StringBuffer sb = new StringBuffer();
        int dot = str.indexOf(".");
        if (dot < 0)
            dot = str.length();

        String zhengshu = str.substring(0, dot);
        sb.append(numberToZH(Long.parseLong(zhengshu), fan));
        if (dot != str.length())
        {
            sb.append("点");
            String xiaoshu = str.substring(dot + 1);
            for (int i = 0; i < xiaoshu.length(); i++)
            {
                if (fan)
                {
                    sb.append(zhnum_0.charAt(Integer.parseInt(xiaoshu.substring(
                        i, i + 1))));
                }
                else
                {
                    sb.append(zhnum.charAt(Integer.parseInt(xiaoshu.substring(i,
                        i + 1))));
                }
            }
        }
        String s = new String(sb);
        if (s.startsWith("零"))
            s = s.substring(1);
        if (s.startsWith("一十"))
            s = s.substring(1);
        while (s.endsWith("零"))
        {
            s = s.substring(0, s.length() - 1);
        }
        if (s.endsWith("点"))
            s = s.substring(0, s.length() - 1);
        return s;
    }

    public static String numberToRMB(double rmb)
    {
        String strRMB = "" + rmb;
        DecimalFormat nf = new DecimalFormat("#.#");
        nf.setMaximumFractionDigits(2);
        strRMB = nf.format(rmb).toString();
        strRMB = numberToZH(strRMB, true);
        if (strRMB.indexOf("点") >= 0)
        {
            strRMB = strRMB + "零";
            strRMB = strRMB.replaceAll("点", "圆");
            String s1 = strRMB.substring(0, strRMB.indexOf("圆") + 1);
            String s2 = strRMB.substring(strRMB.indexOf("圆") + 1);
            strRMB = s1 + s2.charAt(0) + "角" + s2.charAt(1) + "分整";
        }
        else
        {
            strRMB = strRMB + "圆整";
        }
        return "人民币(大写):" + strRMB;
    }

    public static void main(String[] args)
    {
        System.out.println(numberToRMB(342345.96));
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值