首页 > 其他 > 详细

数字转汉子数字

时间:2020-10-23 23:36:10      阅读:28      评论:0      收藏:0      [点我收藏+]

`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( digits.length==2&&digits[0]==1&&i==0?"":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();
    }
    return sb.toString();
}

public static void main(String[] args) {
    for(int i=0;i<10000;i++) {
        System.out.println(digital2Chinese(i));
    }
}

}`

数字转汉子数字

原文:https://www.cnblogs.com/blueberry006/p/13866308.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!