need help converting numbers to word in Java

不羁的心 提交于 2019-12-02 05:01:28

According to your comments, the problem is that you're getting the ones output for numbers between 11-19.

Looking at your tensAndOnesToString() method, it checks whether ten == 1, for the purpose of identifying the teens numbers. So, why don't you put a similar check in your if(d4 != 0) line,

    if(d4 != 0 && d3 != 1) // CHANGED THIS LINE
    {
        if(strOut.equals(""))
            strOut = digitsToString(one);
        else
        {
            strOut = strOut +" "+ digitsToString(one);
        }
    }

So now it will only output a one number if it (d4) isn't 0, and if the tens (d3) isn't 1.

In this short code, I'd used recursive functions to make it simple answer with good performance. I divide the given number to small pieces of math understandable values. This type of divide is math based, which do mostly by the CPU and use smaller amount of memory.

public class NumbersLetter {

    private java.util.HashMap<Long, String> hashMap = new java.util.HashMap<>(34);
    private long[] dividers = {
            1_000_000_000_000_000_000L, 1_000_000_000_000_000L, 1_000_000_000_000L,
            1_000_000_000L, 1_000_000L, 1_000L, 100L, 10L, 1L};

    public String getNaturalNumbersLetter(long number) {
        String regex = "[0-9]+";
        if (!Long.toString(number).matches(regex)) {
            number = 0;
        }
        return divideNumber(number);
    }

    private String getDigitLetter(long digit) {
        return hashMap.get(digit);
    }

    private String addSeparation(boolean addSeperator) {
        if (addSeperator) return " and ";
        return "";
    }

    private String divideNumber(long digit) {
        //Check if the number is a pure number
        // and mapped in our @hashMap
        if (hashMap.containsKey(digit)) {
            return " " + getDigitLetter(digit);
        }
        // Start to divide the given number
        // to small pieces of math understandable values
        // This type of divide is math based
        // which do mostly by the CPU and
        // use small amount of RAM.
        for (long i : dividers) {
            /**
             * Start divide the given number to smaller pieces
             * for example will change 4,321 to
             * 4000 , 300 ,20 & 1
             * this is one of those formats which is human readable.
             * The important thing about this design is that
             * I use calculation instead of buffering strings.
             * */
            if ((digit >= i)) {
                if (digit % i == 0) {
                    //
                    return divideNumber(digit / i) + divideNumber(i);
                } else {
                    if ((digit / i) == 1) {
                        return divideNumber(digit / i)
                                + divideNumber((digit / i) * i)
                                + divideNumber(digit % i);
                    } else {
                        return divideNumber((digit / i) * i)
                                + divideNumber(digit % i);
                    }
                }
            }
        }
        return "";
    }

    public NumbersLetter() {
        // NumbersLetter Constructor
        hashMap.put(0L, "Zero");
        hashMap.put(1L, "One");
        hashMap.put(2L, "Two");
        hashMap.put(3L, "Three");
        hashMap.put(4L, "Four");
        hashMap.put(5L, "Five");
        hashMap.put(6L, "Six");
        hashMap.put(7L, "Seven");
        hashMap.put(8L, "Eight");
        hashMap.put(9L, "Nine");
        hashMap.put(10L, "Ten");
        hashMap.put(11L, "Eleven");
        hashMap.put(12L, "Twelve");
        hashMap.put(13L, "Thirteen");
        hashMap.put(14L, "Fourteen");
        hashMap.put(15L, "Fifteen");
        hashMap.put(16L, "Sixteen");
        hashMap.put(17L, "Seventeen");
        hashMap.put(18L, "Eighteen");
        hashMap.put(19L, "Nineteen");
        hashMap.put(20L, "Twenty");
        hashMap.put(30L, "Thirty");
        hashMap.put(40L, "Forty");
        hashMap.put(50L, "Fifty");
        hashMap.put(60L, "Sixty");
        hashMap.put(70L, "Seventy");
        hashMap.put(80L, "Eighty");
        hashMap.put(90L, "Ninety");
        hashMap.put(100L, "Hundred");
        hashMap.put(1_000L, "Thousand");
        hashMap.put(1_000_000L, "Million");
        hashMap.put(1_000_000_000L, "Billion");
        hashMap.put(1_000_000_000_000L, "Trillion");
        hashMap.put(1_000_000_000_000_000L, "Quadrillion");
        hashMap.put(1_000_000_000_000_000_000L, "Quintillion");
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!