zoukankan      html  css  js  c++  java
  • 数字转汉子数字

    数字转汉字代码如下:

    import java.math.BigDecimal;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;
    
    public class DigitalUtils {
    
        private static String[] NUMBER_ZH = new String[]{"零","一","二","三","四","五","六","七","八","九","十"};
        //最大支持9千兆
        private static String[] NUMBER_UNIT = new String[]{"","十","百","千","万","十","百","千","亿","十","百","千","兆","十","百","千"};
    
        /**
         * @param number
         * @return
         */
        public static String digital2Chinese(long number){
            if(number==0){
                return NUMBER_ZH[0];
            }
            Long value = number;
            Boolean isNegative = false;
            if (value < 0){
                value = -value;
                isNegative= true;
            }
            BigDecimal bigDecimal = BigDecimal.valueOf(value);
    
            String valueStr = value.toString();
    
            Integer [] digits = new Integer[valueStr.length()];
    
            for (int i=0;i<valueStr.length();i++){ //循环数存储每一位数字,从低到高
                digits[i] = bigDecimal.divideAndRemainder(BigDecimal.valueOf(10))[1].intValue();  //value.intValue()%10;
                bigDecimal = bigDecimal.divide(BigDecimal.valueOf(10));
            }
            StringBuilder sb = new StringBuilder();
    
            List<Integer> arr=Arrays.asList(digits);
            Collections.reverse(arr);  // 从高到低
            digits=arr.toArray(new Integer[]{});
    
            Boolean flush = false;
            Boolean needFilling = true;
    
            if (digits.length > 16){
                throw new ArrayIndexOutOfBoundsException("数字太大了,超出汉字可读范围");
            }
    
            for (int i=0;i<digits.length;i++){
                if (digits[i].equals(0)){
                    if (needFilling){
                        if ( digits.length-i-1  == 4){
                            sb.append("万");
                            needFilling = false;
                        }
                        if ( digits.length-i-1 == 8){
                            sb.append("亿");
                            needFilling = false;
                        }
                        if ( digits.length-i-1 == 12){
                            sb.append("兆");
                            needFilling = false;
                        }
                    }
    
                    flush = true;
                    continue;
                }
                if (flush){
                    sb.append("零");
                    flush = false;
                }
                sb.append( NUMBER_ZH[ digits[i] ] ).append( NUMBER_UNIT[digits.length-i-1] );
                if (Arrays.asList(4,8,12).contains( digits.length-i-1 )){
                    needFilling = false;
                }else{
                    needFilling = true;
                }
            }
    
            if (isNegative){
                return "负"+sb.toString();
            }
            String chinese=sb.toString();
            if (chinese.indexOf("一十") == 0) {
                chinese = chinese.replaceFirst("一十", "十");
            }
            return chinese;
        }
    
        public static void main(String[] args) {
            System.out.println("toChinese:"+digital2Chinese(312475));
        }
    }
    
  • 相关阅读:
    Tomcat 启动很慢?
    CentOS 下 安装 JDK8
    CentOS 下 安装 nginx
    SpringBoot 之 打war包
    Spring MVC处理异常有3种方法
    springboot 之 使用jetty web容器
    IDEA 中,编译后不拷贝 mybatis 配置的 mapper 的 xml 文件
    js 鼠标点击页面出现文字
    PHP 获取天气
    js 必须为字母或下划线, 一旦创建不能修改
  • 原文地址:https://www.cnblogs.com/blueberry006/p/13866308.html
Copyright © 2011-2022 走看看