KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > util > English


1 package org.apache.lucene.util;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19
20 public class English {
21
22   public static String JavaDoc intToEnglish(int i) {
23     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
24     intToEnglish(i, result);
25     return result.toString();
26   }
27
28   public static void intToEnglish(int i, StringBuffer JavaDoc 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) { // billions
38
intToEnglish(i/1000000000, result);
39       result.append("billion, ");
40       i = i%1000000000;
41     }
42     if (i >= 1000000) { // millions
43
intToEnglish(i/1000000, result);
44       result.append("million, ");
45       i = i%1000000;
46     }
47     if (i >= 1000) { // thousands
48
intToEnglish(i/1000, result);
49       result.append("thousand, ");
50       i = i%1000;
51     }
52     if (i >= 100) { // hundreds
53
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 JavaDoc[] args) {
99     System.out.println(intToEnglish(Integer.parseInt(args[0])));
100   }
101
102 }
103
Popular Tags