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实现人民币数字和大写转换

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

人民币数字转大写.java

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

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

SQL SERVER函数将人民币数字转换成大写形式

CREATE FUNCTION [dbo].[f_num_chn] (@num numeric(14,5)) RETURNS varchar(100) WITH ENCRYPTION A...

Javascript:将人民币数字转换成大写形式

用JavaScript编写了一功能函数,完成如下功能:1、对一给定字符串,如:1234.55,转换成正确的中文货币描述:如:人民币壹仟贰佰叁拾四元五角五分2、输入的字符串形式可以是以下几种:带分隔符的...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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