the amount of capital java digital conversion algorithm

According to the amount of RMB capital standards, the conversion there are several points to note:

When the middle of Arabic numerals "0" in the middle Chinese capital amount can just write a "zero" word. As ¥ 1,409.50, should be written RMB one thousand Sibai zero Nine round Wu Jiao.
There are several intermediate consecutive Arabic numerals "0" in the middle Chinese capital amount can just write a "zero" character, such as ¥ 6,007.14, should be written yuan Lu One thousand zero qi round the corner store points.
Arabic numeral ten thousand or dollar amount bit is "0", or Digital Intermediate There are several consecutive "0" ten thousand Yuan bit is "0", but thousand bits, angular position is not "0", the Chinese capital in the amount of only one word can be written to zero, you can not write "zero" character, such as ¥ 1,680.32, should be written RMB one thousand Lu Bai> Delta II Bashi yuan zero points, or written RMB one thousand yuan Lu Bai Bashi triangle II points; and if ¥ 107,000.53, should be written RMB yuan and 100,000 of Qi Qian Wu Jiao-third, or written RMB 100,000 of Qi Qian Wu Jiao-third round.
Arab amount of digital angular position is "0", while sub-bit is not "0", the amount of Chinese capital "meta" behind should write "zero" word. As ¥ 16,409.02, 1,000 land should be written ten thousand RMB yuan Sibai zero zero II Nine minutes; another example ¥ 325.04, should be written as zero circle Twenty three hundred RMB store points.
Capital amount to "dollar" until after "dollars" should be written on the "whole" or "positive" word, after "angle" can not write "whole" or "positive" word, not after "minute" write "whole" or "positive" word.

The basic idea, a group of four each, denoting thousands, hundreds, pick up, and then add million million, or round, so if there are duplicate 0, want to ignore

public static String moneyToChinese(BigDecimal i_money) {
        if(i_money.equals(BigDecimal.ZERO)){
            return "零圆整";
        }
        if (i_money.doubleValue() >= 100000000 || i_money.doubleValue() < 0.01) {
            return "";
        }
        i_money = i_money.setScale(2, RoundingMode.HALF_UP);
        String numStr = i_money.toString();
        int pointPos = numStr.indexOf('.');
        String s_int = null; //整数部分
        String s_point = null; //小数部分
        if (pointPos >= 0) {
            s_int = numStr.substring(0, pointPos);
            s_point = numStr.substring(pointPos + 1);
        } else {
            s_int = numStr;
        }
        StringBuilder sb = new StringBuilder();
        if(!"0".equals(s_int)){
            int groupCount = (int) Math.ceil(s_int.length() / 4.0);
            for (int group = 0; group < groupCount; group++) {
                boolean zeroFlag = true;
                boolean noZeroFlag = false;
                int start = (s_int.length() % 4 == 0 ? 0 : (s_int.length() % 4 - 4)) + 4 * group;
                for (int i = 0; i < 4; i++) {
                    if (i + start >= 0) {
                        int value = s_int.charAt(i + start) - '0';
                        if (value > 0) {
                            sb.append(CN_UPPER_NUMBER[value]);
                            if (i < 3) {
                                sb.append(CN_UPPER_UNIT[i]);
                            }
                            zeroFlag = true;
                            noZeroFlag = true;
                        } else if (zeroFlag) {
                            sb.append('零');
                            zeroFlag = false;
                        }
                    }
                }
                if(sb.charAt(sb.length() - 1) == '零'){
                    sb.deleteCharAt(sb.length() - 1);
                }
                if(noZeroFlag || groupCount - group == 1){
                    sb.append(CN_GROUP[groupCount - group - 1]);
                }
            }
        }
        if (s_point == null || "00".equals(s_point)) {
            sb.append('整');
        }else{
            int j = s_point.charAt(0) - '0';
            int f = s_point.charAt(1) - '0';
            if(j > 0){
                sb.append(CN_UPPER_NUMBER[j]).append('角');
                if(f != 0){
                    sb.append(CN_UPPER_NUMBER[f]).append('分');
                }
            }else if("0".equals(s_int)){
                sb.append(CN_UPPER_NUMBER[f]).append('分');
            }else {
                sb.append('零').append(CN_UPPER_NUMBER[f]).append('分');
            }
        }
        return sb.toString();
    }
 
 
    private static final char[] CN_UPPER_NUMBER = "零壹贰叁肆伍陆柒捌玖".toCharArray();
    private static final char[] CN_UPPER_UNIT = "仟佰拾".toCharArray();
    private static final char[] CN_GROUP = "圆万亿".toCharArray();

Transfer from https://www.cnblogs.com/xcr1234/p/9140867.html

Guess you like

Origin www.cnblogs.com/ibigboy/p/10980861.html