Click here to Skip to main content
16,015,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Quote:
Create a function that takes a numeral (just digits without separators (e.g. 19093 instead of 19,093) and returns the standard way of reading a number, complete with punctuation.

Examples
sayNumber(0) ➞ "Zero."

sayNumber(11) ➞ "Eleven."

sayNumber(1043283) ➞ "One million, forty three thousand, two hundred and eighty three."

sayNumber(90376000010012) ➞ "Ninety trillion, three hundred and seventy six billion, ten thousand and twelve."
Notes
Must read any number from 0 to 999,999,999,999,999.


What I have tried:

class SayTheNumber {
    // These are our numbers from which we will extract our words.
    private static String[] less_than_twenty = {"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
    
    private static String[] tens = {"","","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
    
    private static String[] chunks = {"","Thousand","Million","Billion","Trillion"};
    
    // This function takes the numerical value and breaks it into parts with 3 digit each.
    // Then it passes it to next function coupled with it that will build the string for it.
    public static String sayNumber(long num) {
       if(num==0) return "Zero.";
        
        String ans = new String();
        
        int index=0;
        while(num>0){
            if(num%1000!=0){
                ans = convertThreeDigit(num%1000) + chunks[index]+", " + ans;
            }
            index++;
            num/=1000;
        }
        // for perfecting our punctuation, we will use substring and trim methods.
        return ans.trim().substring(0,ans.length()-3)+".";
        
    }

    // This function recieves the 3 digit number and builds the string.
    // Its a recursive function.
    private static String convertThreeDigit(long num){
        if(num==0)return "";
        if(num<20) return less_than_twenty[(int)num]+" ";
        
        else if(num<100) return tens[(int)num/10] + " " + convertThreeDigit(num%10);
        
        else return less_than_twenty[(int)num/100] + " "+"Hundred"+" "+convertThreeDigit(num%100);
    }

    // Finally our driver class to test our inputs.
    public static void main(String[] args) {
        long n1 = 0;
        long n2 = 11;
        int n3 = 1254367125;
        System.out.println(sayNumber(n1)+"\n"+sayNumber(n2)+"\n"+sayNumber(n3));
    }
}
Posted
Updated 7-Oct-22 5:42am
Comments
Richard MacCutchan 7-Oct-22 11:04am    
Change n3 from int to long. And remember to add the "L" suffix to long numbers.
Thandeka Zulu 8-Oct-22 5:15am    
@Richard Thank you so much.
Richard MacCutchan 8-Oct-22 11:02am    
You are welcome. At least you know that all the rest of the code worked correctly.

1 solution

Java long variables can hold any value from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (inclusive) because it uses 64 bits to store the value.
An integer on the other hand is 32 bits: -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive)

If you store a long value in an integer - even temporarily - it is truncated, and anything that doesn't fit cannot be recovered.

Use the debugger to follow through exactly what you code is doing, and you should soon spot exactly where the problem occurs!
 
Share this answer
 
Comments
Thandeka Zulu 8-Oct-22 5:14am    
@OriginalGriff Noted.Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900