Here is the Java Program :
package Practice.Program;
public class Num2Words {
private static long number;
private static String words = new String("");
public Num2Words(long n){ number = n; }
static String[] tp = {"", "thousand ", "million ", "billion ", "trillion ", "quadrillion ", "pentillion "};
static String[] od = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
static String[] td = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
static String[] tn = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
public String toWords(){
words = Long.toString(number);
int l = words.length();
boolean n = false;
if (number < 0){ n = true; number *= -1; --l;}
breaker : for (byte LOL = 0; LOL < 1; LOL++){
if (StringHasChar(words, '.')){ words = "Decimals not supported."; break breaker;}
if (l>19){ words = "too big"; break breaker;}
if (l==1) words = oneDigit(number); // one digit
else if (l==2) words = twoDigits(number); // two digits
else if (l==3) words = threeDigits(number); // three digits
else words = nDigits(number); // multiple digits
}
if (n == true) {words = "Minus "+words; number *= -1;}
return words;
}
private static String oneDigit(long n){
char[] ns = Long.toString(n).toCharArray();
return od[(char2num(ns[0]))];
}
private static String twoDigits(long n){
String s = "";
char[] ns = Long.toString(n).toCharArray();
if (n < 20){
s = td[(char2num(ns[1]))];
} else if (n < 100){
s = tn[char2num(ns[0])];
if (char2num(ns[1])!=0){ s += " " + od[char2num(ns[1])]; }
}
return s;
}
private static String threeDigits(long n){
String s = "", is = Long.toString(n);
long[] ns = String2longArray(is);
s = od[(int)(ns[0])] + " hundred";
if (ns[1]!=0&&ns[2]!=0){
long i = (ns[1])*10+(ns[2]);
s += " " + twoDigits(i);
} else {
if (ns[1]!=0) s += " " + twoDigits((ns[1]*10));
if (ns[2]!=0) s += " " + oneDigit(ns[2]);
}
return s;
}
private static String nDigits(long n){
String s = threeDigits(n);
int tpi = ((digits(n)+2)/3)-1, rtpi = 0;
long[] tfr = threeFromRight(n);
s = "";
for (; tpi >= 0; tpi--, rtpi++){
s = new Num2Words(tfr[tpi]).toWords() + " " + tp[rtpi] + s;
}
return s;
}
private static int digits(long n){
long ans = 0;
for (; n>0; n/=10, ans++);
return (int)(ans);
}
private static long[] threeFromRight(long n){
int tpi = (digits(n)+2)/3, nl = digits(n);
long[] ans=new long[tpi], ia=String2longArray(Long.toString(n)), ta=new long[3];
for (int i=nl, t=2, c=0, l=tpi*3; l >= 0; l--, c++, i--, t--){
try {ta[t] = ia[i-1];}
catch (ArrayIndexOutOfBoundsException e){ta[t]=0;}
if (((c+1)%3)==0){
--tpi;
ans[tpi] = longArrays2long(ta);
t = 3;
c = -1;
}
}
return ans;
}
private static int char2num(char c){
char[] ca = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
byte ia[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, ans = -1;
for (byte i = 0; i < 10; i++){ if (c == ca[i]){ ans = ia[i]; break;} }
return ans;
}
private static long[] String2longArray(String s){
char[] ca = s.toCharArray();
byte l = (byte)s.length();
long[] ans = new long[l];
for (byte i = 0; i < l; i++){ ans[i] = char2num(ca[i]); }
return ans;
}
private static long longArrays2long(long[] ia){
long ans = 0;
try { for (int i = 0; i >= 0; ans += ia[i], ans *= 10, ++i); }
catch (ArrayIndexOutOfBoundsException e){}
ans/=10;
return ans;
}
private static boolean StringHasChar(String s, char c){
char[] sca = s.toCharArray();
byte l = (byte)(s.length());
boolean ans = false;
for (byte i = 0; i > l; i++){
if (sca[i] == c) ans = true;
}
return ans;
}
public static void main(String[] args){
long n = -1053664648543756767l;
String words = new Num2Words(n).toWords();
System.out.println(n + " : " + words);
}
}
I've also posted my program as an answer to a question in Stack Overflow
.
Would anyone like to help me out with putting some procedures into MIT AI2 ?
If Yes
How you can help me :-
- Make an aia.
- Make the procedures.
- Send the aia.
Helping me is fully your own wish. I'd not force anyone.
Thank You.