Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Integer to English Words
//'main' method must be in a class 'Rextester'. //Compiler version 1.8.0_111 import java.util.*; import java.lang.*; //Integer to English Words /* Input: 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven" Input: 1234567891 Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One" */ class Rextester { private static final String[] belowTen = new String[]{"","one","two","three","four","five","six","seven","eight","nine"}; private static final String[] belowTwenty = new String[]{"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"}; private static final String[] belowHundred = new String[]{"","ten","twenty","Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; public static void main(String args[]) { System.out.println(numberToWords(1234567)); } public static String numberToWords(int num) { if(num==0) return "Zero"; return helper(num); } public static String helper(int num){ String out = new String(); if (num<10) out = belowTen[num]; else if (num<20) out = belowTwenty[num-10]; else if (num<100) out = belowHundred[num/10]+" "+helper(num%10); else if (num<1000) out = helper(num/100) + " hundred "+ helper(num%100); else if (num<1000000) out = helper(num/1000) + " thousand "+helper(num%1000); else if (num<1000000000) out = helper(num/1000000) + " million "+ helper(num%1000000); else out = helper(num/1000000000) + " billion "+helper(num%1000000000); return out.trim(); } }
run
|
edit
|
history
|
help
0
// Java Coding Challenge - 08: Reversing a Number using String
Round Price
class and name
MinMaxArray
test1
calc exp gxb2
Java Class and Object
Человечки
String Class Methods
Prime Factors