Created
June 29, 2018 03:24
-
-
Save mithuns/612bbf521415836fabbcb08eb29e935a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ms; | |
public class MoneyToWords { | |
public static final String[] units = { "", "One", "Two", "Three", "Four", | |
"Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", | |
"Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", | |
"Eighteen", "Nineteen" }; | |
public static final String[] tens = { | |
"", // 0 | |
"", // 1 | |
"Twenty", // 2 | |
"Thirty", // 3 | |
"Forty", // 4 | |
"Fifty", // 5 | |
"Sixty", // 6 | |
"Seventy", // 7 | |
"Eighty", // 8 | |
"Ninety" // 9 | |
}; | |
public static String convert(int n) { | |
if(n<0) return "Minus" + convert(-n); | |
if(n<20) return units[n]; | |
if(n<100) return tens[n/10]+" "+units[n%10]; | |
if(n<1000) return units[n/100]+" hundred"+ (n%100>0?" and "+convert(n%100):""); | |
if(n<100000) return units[n/1000]+" thousand"+ (n%1000>0?" and "+convert(n%1000):""); | |
if(n<10000000) return units[n/1000000]+" million"+ (n%1000000>0?" and "+convert(n%1000000):""); | |
return "Sorry, the number is too big for me right now, I am only 5 years old, you know"; | |
} | |
public static void main(String[] args) { | |
// System.out.println(0+"--"+convert(0)); | |
// System.out.println(1+"--"+convert(1)); | |
// System.out.println(10+"--"+convert(10)); | |
// System.out.println(20+"--"+convert(20)); | |
// System.out.println(21+"--"+convert(21)); | |
// System.out.println(1000+"--"+convert(1000)); | |
// System.out.println(9999+"--"+convert(9999)); | |
// System.out.println(9000000+"--"+convert(9000000)); | |
System.out.println(9999999+"--"+convert(9999999)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment