1 package org.apache.lucene.util; 2 3 18 19 20 public class English { 21 22 public static String intToEnglish(int i) { 23 StringBuffer result = new StringBuffer (); 24 intToEnglish(i, result); 25 return result.toString(); 26 } 27 28 public static void intToEnglish(int i, StringBuffer result) { 29 if (i == 0) { 30 result.append("zero"); 31 return; 32 } 33 if (i < 0) { 34 result.append("minus "); 35 i = -i; 36 } 37 if (i >= 1000000000) { intToEnglish(i/1000000000, result); 39 result.append("billion, "); 40 i = i%1000000000; 41 } 42 if (i >= 1000000) { intToEnglish(i/1000000, result); 44 result.append("million, "); 45 i = i%1000000; 46 } 47 if (i >= 1000) { intToEnglish(i/1000, result); 49 result.append("thousand, "); 50 i = i%1000; 51 } 52 if (i >= 100) { intToEnglish(i/100, result); 54 result.append("hundred "); 55 i = i%100; 56 } 57 if (i >= 20) { 58 switch (i/10) { 59 case 9 : result.append("ninety"); break; 60 case 8 : result.append("eighty"); break; 61 case 7 : result.append("seventy"); break; 62 case 6 : result.append("sixty"); break; 63 case 5 : result.append("fifty"); break; 64 case 4 : result.append("forty"); break; 65 case 3 : result.append("thirty"); break; 66 case 2 : result.append("twenty"); break; 67 } 68 i = i%10; 69 if (i == 0) 70 result.append(" "); 71 else 72 result.append("-"); 73 } 74 switch (i) { 75 case 19 : result.append("nineteen "); break; 76 case 18 : result.append("eighteen "); break; 77 case 17 : result.append("seventeen "); break; 78 case 16 : result.append("sixteen "); break; 79 case 15 : result.append("fifteen "); break; 80 case 14 : result.append("fourteen "); break; 81 case 13 : result.append("thirteen "); break; 82 case 12 : result.append("twelve "); break; 83 case 11 : result.append("eleven "); break; 84 case 10 : result.append("ten "); break; 85 case 9 : result.append("nine "); break; 86 case 8 : result.append("eight "); break; 87 case 7 : result.append("seven "); break; 88 case 6 : result.append("six "); break; 89 case 5 : result.append("five "); break; 90 case 4 : result.append("four "); break; 91 case 3 : result.append("three "); break; 92 case 2 : result.append("two "); break; 93 case 1 : result.append("one "); break; 94 case 0 : result.append(""); break; 95 } 96 } 97 98 public static void main(String [] args) { 99 System.out.println(intToEnglish(Integer.parseInt(args[0]))); 100 } 101 102 } 103 | Popular Tags |