• JAVA处理数字与中文数字互转(最大处理数字不超过万兆即:9999999999999999.9999)


    package practice;
    
    import java.util.Arrays;
    
    /**
     * 数字与中文数字互转(最大处理数字不超过万兆即:9999999999999999.9999)
     * @author Archimedes
     * @Date 6 7 19
     */
    public class ChineseNumberAndNumber {
        
        private static final char NUMBER_CONSTRAINT[] = {'9', '8', '7', '6', '5', '4', '3', '2', '1', '0', '.'};
        
        private static final String[] DECIMAL_UNIT = {"角", "分", "厘", "毫"};
        
        private static final String[] CHINESE_NUMBER = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
        
        private static final String[] INTEGER_UNIT = {"", "万", "亿", "兆"};
        
        private static final String[] INTEGER_UNIT1 = {"", "拾", "佰", "仟"};
        
        /**
         * 数字转中文数字
         * @param number
         * @return
         */
        public static String numberToChineseNumber(String number) {
            //数字有效性判断
            //先判断number中的数字是否在NUMBER_CONSTRAINT[]中
            int quantity = NUMBER_CONSTRAINT.length - 1;
            for (int i = 0; i < number.length(); i++) {
                char unit = number.charAt(i);
                for (int j = 0; j < NUMBER_CONSTRAINT.length; j++) {
                    if (unit == NUMBER_CONSTRAINT[j]) break;
                    if (j == quantity) return "请输入有效数字";
                }
            }
            //再判断number中小数点是否不超过1个且小数点不在第一位和最后一位
            int quantity1 = 0;
            for (int i = 0; i < number.length(); i++) {
                if ('.' == number.charAt(i) && (i == 0 || i == number.length() - 1)) return "1请输入有效数字";
                if ('.' == number.charAt(i)) {
                    ++quantity1;
                    if (quantity1 == 2) return "2请输入有效数字";
                }
            }
            //最后做数字范围的有效判断,并且把诸如0.000000000000000的数字转换为0
            if (quantity1 == 0) {
                number = headNumberIsZero(number);
                if (number.length() > 16) return "3请输入有效数字";
            } else {
                String[] split = number.split("[.]");
                //处理整数部分
                split[0] = headNumberIsZero(split[0]);
                if (split[0].length() > 16) return "4请输入有效数字";
                //处理小数部分
                if (split[1].length() > 4) {
                    for (int i = 4; i < split[1].length(); i++) {
                        if ('0' != split[1].charAt(i)) return "5请输入有效数字";
                    }
                    split[1] = split[1].substring(0, 4);
                }
                for (int i = split[1].length() - 1; i >= 0; i--) {
                    if ('0' == split[1].charAt(i)) split[1] = split[1].substring(0, i);
                    else break;
                }
                //合并整数与小数
                if (split[1].isEmpty()) number = split[0];
                else number = split[0] + "." + split[1];
            }
            //数字有效性判断完毕
            
            //数字转中文数字
            String[] result = {"", ""};
            String[] split = number.split("[.]");
            //处理小数部分
            if (split.length == 2) {
                int c = 0; //定义标识用于判断前一位是否为0
                for (int i = 0; i < split[1].length(); i++) {
                    if (c == 0 && ('0' == split[1].charAt(i))) {
                        result[1] += CHINESE_NUMBER[0];
                        c = 1;
                    } else if ('0' != split[1].charAt(i)) {
                        result[1] += (CHINESE_NUMBER[Integer.parseInt(String.valueOf(split[1].charAt(i)))] + DECIMAL_UNIT[i]);
                        c = 0;
                    }
                }
            }
            //处理整数部分
            //把整数部分从左到右每4个一组放入数组
            String[] integerPart = new String[(split[0].length() - 1) / 4 + 1];
            for (int i = split[0].length() - 1; i >= 0; i -= 4) {
                if (i - 3 >= 0) integerPart[i / 4] = split[0].substring(i - 3, i + 1);
                else integerPart[i / 4] = split[0].substring(0, i + 1);
            }
            System.out.println(Arrays.toString(integerPart));
            //转换
            int length = integerPart.length;
            int c = 0; //用于判断前一位是否为0
            if (length > 1) {
                int l = 1; ////用于判断上一次计数单位是否全为0(0表示全为0)
                for (int i = 0; i < integerPart.length; i++) {
                    int x = 0; //用于判断计数单位是否全为0
                    int length1 = integerPart[i].length();
                    for (int j = 0; j < length1; j++) {
                        if (c == 0 && '0' != integerPart[i].charAt(j)) {
                            String temp = CHINESE_NUMBER[Integer.parseInt(String.valueOf(integerPart[i].charAt(j)))]
                                    + INTEGER_UNIT1[length1 - 1 -j];
                            result[0] += temp;
                            x = 1;
                        } else if ('0' == integerPart[i].charAt(j)) {
                            c = 1;
                        } else if (c == 1 && j == 0 && '0' != integerPart[i].charAt(j)) {
                            if (l == 0) result[0] += "零";
                            String temp = CHINESE_NUMBER[Integer.parseInt(String.valueOf(integerPart[i].charAt(j)))]
                                    + INTEGER_UNIT1[length1 - 1 -j];
                            result[0] += temp;
                            c = 0; x = 1;
                        } else if (c == 1 && j != 0 && '0' != integerPart[i].charAt(j)) {
                            String temp = "零" + CHINESE_NUMBER[Integer.parseInt(String.valueOf(integerPart[i].charAt(j)))]
                                    + INTEGER_UNIT1[length1 - 1 -j];
                            result[0] += temp;
                            c = 0; x = 1;
                        }
                    }
                    l = x; //把此次循环的计数单位是否全为0的结果赋值给变量,用于下次循环判断
                    if (x == 1) result[0] += INTEGER_UNIT[length - 1 - i];
                }
                result[0] += "元";
            } else {
                int length1 = integerPart[0].length();
                for (int i = 0; i < length1; i++) {
                    if (c == 0 && ('0' != integerPart[0].charAt(i))) {
                        String temp = CHINESE_NUMBER[Integer.parseInt(String.valueOf(integerPart[0].charAt(i)))]
                                + INTEGER_UNIT1[length1 - 1 - i];
                        result[0] += temp;
                    } else if ('0' == integerPart[0].charAt(i)) {
                        if (length1 == 1) {
                            result[0] += "零";
                        }
                        c = 1;
                    } else if (c == 1 && ('0' != integerPart[0].charAt(i))) {
                        String temp = "零" + CHINESE_NUMBER[Integer.parseInt(String.valueOf(integerPart[0].charAt(i)))]
                                + INTEGER_UNIT1[length1 - 1 - i];
                        result[0] += temp;
                        c = 0;
                    }
                }
                result[0] += "元";
            }
            if (split.length == 1) {
                result[0] += "整";
                return result[0];
            } else {
                if ("零元".equals(result[0])) return result[1];
                else return result[0] + result[1];
            }
            //数字转中文数字完毕
        }
        
        /**
         * 把数字的前几个连续的0去掉,但数字如全为0则保留1个
         * @param number
         * @return
         */
        private static String headNumberIsZero(String number) {
            try {
                if ('0' == number.charAt(0)) return headNumberIsZero(number.substring(1));
                else return number;
            } catch (IndexOutOfBoundsException e) {
                return "0";
            }
        }
        
        private static String headNumberIsZero1(String number) {
            if (number.isEmpty()) return "0";
            if ('0' == number.charAt(0)) return headNumberIsZero(number.substring(1));
            else return number;
        }
        
        private static String headNumberIsZero2(String number) {
            if ('0' != number.charAt(0)) return number;
            else {
                for (int i = 1; i < number.length(); i++) {
                    if ('0' != number.charAt(i)) {
                        number = number.substring(i);
                        break;
                    } else if (i == number.length() -1) number = "0";
                }
            }
            return number;
        }
        
        public static void main(String[] args) {
            String number = numberToChineseNumber("64646405000.033");
            System.out.println(number);
        }
        
    }
  • 相关阅读:
    【SignalR学习系列】6. SignalR Hubs Api 详解(C# Server 端)
    【SignalR学习系列】5. SignalR WPF程序
    小程序商城Mall,打造最佳SpringCloudAlibaba最佳实践
    《SpringCloudDubbo开发日记》(一)Nacos连官方文档都没写好
    Java后端开发工程师是否该转大数据开发?
    动态生成简约MVC请求接口|抛弃一切注解减少重复劳动吧
    主键生成器效率提升方案|基于雪花算法和Redis控制进程隔离
    高并发场景-请求合并(二)揭秘HystrixCollapser-利用Queue和线程池异步实现
    使用TiDB把自己写分库分表方案推翻了
    JMETER并发压测-自定义不同请求参数
  • 原文地址:https://www.cnblogs.com/nearWind/p/11352347.html
Copyright © 2020-2023  润新知