Example 1:
Example 2:
Example 3:
Example 4:
Example 5:
Example 6:
Example 7:
Example 7:
Note: A million is (), that is 1,000,000 (Ten lakhs). A billion is (), that is 1,000,000,000 (100 crore). A trillion is (), that is 1,000,000,000,000 (10 lakh crore). A Quadrillion is (), that is 1,000,000,000,000,000 (100 lakh crore).
1. Divide the Number into Chunks of 3 Digits:
2. Process Each Three-Digit Chunk
3. Recursively Process Each Chunk with Correct Suffix
String[] belowTwenty = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
String[] tens = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
public String numberToWords(int num) {
if(num == 0) {
return "Zero";
} else {
return helperX(num);
}
}
public String helperX(int num) {
StringBuilder sb = new StringBuilder();
if(num < 20)
sb.append(belowTwenty[num]);
else if(num < 100)
sb.append(tens[num / 10]).append(" ").append(helperX(num % 10));
else if(num < 1000)
sb.append(helperX(num / 100)).append(" Hundred ").append(helperX(num % 100));
else if(num < 1000000)
sb.append(helperX(num / 1000)).append(" Thousand ").append(helperX(num % 1000));
else if(num < 1000000000)
sb.append(helperX(num / 1000000)).append(" Million ").append(helperX(num % 1000000));
else
sb.append(helperX(num / 1000000000)).append(" Billion ").append(helperX(num % 1000000000));
return sb.toString().trim();
}
package com.srvivek;
public class IntegerToEnglishWord {
String[] belowTwenty = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
String[] tens = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
final String HUNDRED = "Hundred";
String[] thousands = {"", "Thousand", "Million", "Billion", "Trillion"};
public String integerToEnglish(Integer value) {
if (value == null ) return "";
if(value == 0) return "zero";
int x = value;
StringBuilder sb = new StringBuilder();
int index = 0;
while(value != 0) {
if(value % 1000 > 0) {
String t = helper(value % 1000);
if(index == 0) {
sb.insert(0, t);
} else {
sb.insert(0, t + " " + thousands[index] + (sb.isEmpty() ? "" : " "));
}
}
value /= 1000;
++index;
}
String word = sb.toString();
System.out.printf("Input : %d %nOutput: %s%n", x, word);
return word;
}
/**
* Process value in 3 digit chunks
*/
private String helper(int value) {
if (value == 0) return "";
if (value < 20) return belowTwenty[value];
if(value < 100) return tens[value/10] + " " + helper(value % 10);
else return belowTwenty[value / 100] + " " + HUNDRED + " " + helper(value % 100);
}
}