数字自然读法

package test;

import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.LinkedList;

/**
 *
 * @author Administrator
 */
public class NianShuZi {

    /**
     * 本程序可读x为整数且-10^12 gt x lt 10^12
     */
    private static final String[] cnnumber = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
    private static final String[] cnsep = {"万", "亿"};
    private static final String[] limits = {"十", "百", "千"};

    public static void main(String[] args) {

        String number = "100000101";

        boolean negflag = false;

        if (number.contains("-")) {
            number = number.substring(1);
            negflag = true;
        }
        if (number.length() == 1 && Integer.parseInt(number) == 0) {
            System.out.println(cnnumber[0]);
            return;
        }
        String result = read12Digits(number);
        if (result.startsWith("一十")) {
            result = result.replaceFirst("一十", "十");
        }
        result=result.replaceAll("零+", "零");
        if (negflag) {
            result = "负" + result;
        }
        System.out.println(result);
    }

    //读lt4
    private static String read4Digits(String[] numbers) {
        Deque<String> read = new LinkedList<String>();
        boolean zeroFlag = true;
        for (int i = 0; i < numbers.length; i++) {
            if (i == 0) {
                if (!numbers[i].equals("0")) {
                    read.addFirst(cnnumber[Integer.parseInt(numbers[i])]);
                } else {
                    zeroFlag = false;
                }
            } else {
                if (numbers[i].equals("0")) {
                    if (zeroFlag == true) {
                        read.addFirst(cnnumber[Integer.parseInt(numbers[i])]);
                        zeroFlag = false;
                    }
                } else {
                    read.addFirst(cnnumber[Integer.parseInt(numbers[i])] + limits[i - 1]);
                    zeroFlag = true;
                }
            }
        }
        return read.toString();
    }

    //读lt12
    private static String read12Digits(String number) {
        String[] numbersAll = number.split("(?<=.)(?=.)");
        Collections.reverse(Arrays.asList(numbersAll));
        Deque<String> sb = new LinkedList<String>();
         boolean partzeroflag = true;
        for (int i = 0; i < (numbersAll.length - 1) / 4 + 1; i++) {
            String[] numbers = Arrays.copyOfRange(numbersAll, i * 4, (i + 1) * 4 > numbersAll.length ? numbersAll.length : (i + 1) * 4);
            String readpart = read4Digits(numbers);
            int total = 0;
           
            for (String j : numbers) {
                total += Integer.parseInt(j);
            }
            if (i == 0) {
                if (total != 0) {
                    sb.addFirst(readpart);
                } else {
                    partzeroflag = false;
                }
            } else {
                if (total == 0) {
                    if (partzeroflag == true) {
                        sb.addFirst("零");
                        partzeroflag = false;
                    }
                } else {
                    sb.addFirst(readpart + cnsep[i - 1]);
                    partzeroflag= true;
                }
            }

        }
        String result = sb.toString().replaceAll("\\p{ASCII}", "");
        return result;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值