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 {
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"};
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;
}
return ans.trim().substring(0,ans.length()-3)+".";
}
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);
}
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));
}
}