MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / numberToWords

Method numberToWords

IntegerToEnglishWords.java:5–28  ·  view source on GitHub ↗
(int num)

Source from the content-addressed store, hash-verified

3 String[] belowTwenty = { "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
4 String[] belowHundred = { "", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
5 public String numberToWords(int num) {
6 if(num==0){
7 return "Zero";
8 }
9 if(num<10){
10 return belowTen[num];
11 }
12 if(num<20){
13 return belowTwenty[num-10];
14 }
15 if(num<100){//74, 70
16 return belowHundred[num/10] + (num%10!=0?" " + belowTen[num%10]:"");
17 }
18 if(num<1000){ //9 00
19 return belowTen[num/100] + " Hundred" + (num%100!=0?" " + numberToWords(num%100):"");
20 }
21 if(num<1000000){ //9000 -- 999 000
22 return numberToWords(num/1000) + " Thousand" + (num%1000!=0?" " + numberToWords(num%1000):"");
23 }
24 if(num<1000000000){ //999 129786
25 return numberToWords(num/1000000) + " Million" + (num%1000000!=0?" " + numberToWords(num%1000000):"");
26 }
27 return numberToWords(num/1000000000) + " Billion" + (num%1000000000!=0?" " + numberToWords(num%1000000000):"");
28 }
29}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected