java 转换数字为人民币大写

原创 2012年03月31日 14:26:48
java 转换数字为人民币大写

import java.text.DecimalFormat;


public class MoneyUtil {
 /*
     * 将小写的人民币转化成大写
     */
    public static String convertToChineseNumber(double number)
    {
        StringBuffer chineseNumber = new StringBuffer();
        String [] num={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
        String [] unit = {"分","角","圆","拾","佰","仟","万","拾","佰","仟","亿","拾","佰","仟","万"};
        String tempNumber = String.valueOf(Math.round((number * 100)));
        int tempNumberLength = tempNumber.length();
        if ("0".equals(tempNumber))
        {
            return "零圆整";
        }
        if (tempNumberLength > 15)
        {
            try {
                throw new Exception("超出转化范围.");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        boolean preReadZero = true;    //前面的字符是否读零
        for (int i=tempNumberLength; i>0; i--)
        {
            if ((tempNumberLength - i + 2) % 4 == 0)
            {
                //如果在(圆,万,亿,万)位上的四个数都为零,如果标志为未读零,则只读零,如果标志为已读零,则略过这四位
                if (i - 4 >= 0 && "0000".equals(tempNumber.substring(i - 4, i)))
                {
                    if (!preReadZero)
                    {
                        chineseNumber.insert(0, "零");
                        preReadZero = true;
                    }
                    i -= 3;    //下面还有一个i--
                    continue;
                }
                //如果当前位在(圆,万,亿,万)位上,则设置标志为已读零(即重置读零标志)
                preReadZero = true;
            }
            Integer digit = Integer.parseInt(tempNumber.substring(i - 1, i));
            if (digit == 0)
            {
                //如果当前位是零并且标志为未读零,则读零,并设置标志为已读零
                if (!preReadZero)
                {
                    chineseNumber.insert(0, "零");
                    preReadZero = true;
                }
                //如果当前位是零并且在(圆,万,亿,万)位上,则读出(圆,万,亿,万)
                if ((tempNumberLength - i + 2) % 4 == 0)
                {
                    chineseNumber.insert(0, unit[tempNumberLength - i]);
                }
            }
            //如果当前位不为零,则读出此位,并且设置标志为未读零
            else
            {
                chineseNumber.insert(0, num[digit] + unit[tempNumberLength - i]);
                preReadZero = false;
            }
        }
        //如果分角两位上的值都为零,则添加一个“整”字
        if (tempNumberLength - 2 >= 0 && "00".equals(tempNumber.substring(tempNumberLength - 2, tempNumberLength)))
        {
            chineseNumber.append("整");
        }
        return chineseNumber.toString();
    }

public static String number2BigRMB(double sum) {
        String[] numeral ={"十", "百", "千"};
        String[] bigNumeral = {"亿", "万"};
        String[] measure = {"元", "角", "分"};
        String[] numbers ={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
        StringBuffer result = new StringBuffer();
        int digit = 0;
        for (double temp = sum; temp >= 1; temp /= 10) {
            digit++;
        }
        int loop = 0;
        for (int temp = digit - 4; temp > 0; temp -= 4) {
            loop++;
        }
        int srcLoop = loop;

        boolean acrossLoopZeroFlag = true;

        while (loop >= 0) {
            int doubleBigNumber1 = (int) (sum / Math.pow(10000, loop));
            sum %= Math.pow(10000, loop);

            int bigDigit = 0;
            for (double temp = doubleBigNumber1; temp >= 1; temp /= 10) {
                bigDigit++;
            }
            if (bigDigit < 4 && srcLoop != loop && acrossLoopZeroFlag) {
                result.append(numbers[0]);
                acrossLoopZeroFlag = false;
            } else {
                acrossLoopZeroFlag = true;
            }

            boolean flag = true;
            if (loop > 0) {
                flag = false;
            }
            boolean innerFlag = true;
            if (doubleBigNumber1 == 0) {
                innerFlag = false;
            }
            boolean addZeroFlag = false;

            int i = (int) Math.pow(10, bigDigit - 1);
            for (; i >= 1; i = i / 10, bigDigit--) {
                int doubleNumber = doubleBigNumber1 / i;
                if (doubleNumber > 0) {
                    if (addZeroFlag) {
                        result.append(numbers[0]);
                        addZeroFlag = false;
                    }
                    result.append(numbers[doubleNumber]);
                    if (bigDigit == 1) {
                        if (flag) {
                            result.append(measure[0]);
                        } else {
                            result.append(bigNumeral[loop % 2]);
                        }
                    } else {
                        result.append(numeral[bigDigit - 2]);
                    }
                } else if (bigDigit == 1) {
                    if (flag) {
                        result.append(measure[0]);
                    } else {
                        if (innerFlag) {
                            result.append(bigNumeral[loop % 2]);
                        }
                    }
                } else {
                    addZeroFlag = true;
                }
                doubleBigNumber1 %= i;
            }
            loop--;
        }

        DecimalFormat df = new DecimalFormat("#.##");
        String floatStr = df.format(sum);
        floatStr = floatStr.substring(floatStr.indexOf(".") + 1);
        int floatPart = Integer.parseInt(floatStr);
        int jiao = floatPart / 10;
        int fen = floatPart % 10;
        if (jiao > 0) {
            result.append(numbers[jiao]);
            result.append(measure[1]);
        }
        if (fen > 0) {
            result.append(numbers[fen]);
            result.append(measure[2]);
        }
        return result.toString();
    }


}


java开发_数字转换汉语中人民币的大写_完整版

先看看运行效果: ====================================================== 代码部分 ====...

Java学习小试牛刀之数字金额转换为人民币大写

由于注视部分已经比较

java开发_数字转换汉语中人民币的大写_完整版

做这个应用,源于突然的一个想法:看到发票上面的数字要转换成汉语中人民币的大写 于是就有了下面的这些事儿..... 先看看运行效果: =======================...

java实现人民币数字和大写转换

原文地址:http://liuyanzhao.com/2512.html 直接上代码了,下面代码经测试完美。能解决数字中加0的特例 import java.math.BigDecimal; ...

java 数字转化为人民币大写

  • 2012年09月05日 23:24
  • 46KB
  • 下载

人民币数字转大写.java

  • 2013年10月29日 13:46
  • 6KB
  • 下载

java 数字转中文人民币大写

转自 :http://www.cnblogs.com/hongten/p/hongten_java_money.html 最近有这个需求,看到一篇文章写得很好,特转过来 做...
  • banqgg
  • banqgg
  • 2016年11月17日 13:44
  • 185

java实现阿拉伯数字转人民币大写(精确到分)

这是一个将阿拉伯数字转人民币大写的工具类, 精确到人民币大写分package com.client; import java.math.BigDecimal;/** * Class Descript...

人民币数字转换成汉字大写

  • 2015年07月17日 13:58
  • 7KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:java 转换数字为人民币大写
举报原因:
原因补充:

(最多只允许输入30个字)