001: package org.apache.lucene.util;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: public class English {
021:
022: public static String intToEnglish(int i) {
023: StringBuffer result = new StringBuffer();
024: intToEnglish(i, result);
025: return result.toString();
026: }
027:
028: public static void intToEnglish(int i, StringBuffer result) {
029: if (i == 0) {
030: result.append("zero");
031: return;
032: }
033: if (i < 0) {
034: result.append("minus ");
035: i = -i;
036: }
037: if (i >= 1000000000) { // billions
038: intToEnglish(i / 1000000000, result);
039: result.append("billion, ");
040: i = i % 1000000000;
041: }
042: if (i >= 1000000) { // millions
043: intToEnglish(i / 1000000, result);
044: result.append("million, ");
045: i = i % 1000000;
046: }
047: if (i >= 1000) { // thousands
048: intToEnglish(i / 1000, result);
049: result.append("thousand, ");
050: i = i % 1000;
051: }
052: if (i >= 100) { // hundreds
053: intToEnglish(i / 100, result);
054: result.append("hundred ");
055: i = i % 100;
056: }
057: if (i >= 20) {
058: switch (i / 10) {
059: case 9:
060: result.append("ninety");
061: break;
062: case 8:
063: result.append("eighty");
064: break;
065: case 7:
066: result.append("seventy");
067: break;
068: case 6:
069: result.append("sixty");
070: break;
071: case 5:
072: result.append("fifty");
073: break;
074: case 4:
075: result.append("forty");
076: break;
077: case 3:
078: result.append("thirty");
079: break;
080: case 2:
081: result.append("twenty");
082: break;
083: }
084: i = i % 10;
085: if (i == 0)
086: result.append(" ");
087: else
088: result.append("-");
089: }
090: switch (i) {
091: case 19:
092: result.append("nineteen ");
093: break;
094: case 18:
095: result.append("eighteen ");
096: break;
097: case 17:
098: result.append("seventeen ");
099: break;
100: case 16:
101: result.append("sixteen ");
102: break;
103: case 15:
104: result.append("fifteen ");
105: break;
106: case 14:
107: result.append("fourteen ");
108: break;
109: case 13:
110: result.append("thirteen ");
111: break;
112: case 12:
113: result.append("twelve ");
114: break;
115: case 11:
116: result.append("eleven ");
117: break;
118: case 10:
119: result.append("ten ");
120: break;
121: case 9:
122: result.append("nine ");
123: break;
124: case 8:
125: result.append("eight ");
126: break;
127: case 7:
128: result.append("seven ");
129: break;
130: case 6:
131: result.append("six ");
132: break;
133: case 5:
134: result.append("five ");
135: break;
136: case 4:
137: result.append("four ");
138: break;
139: case 3:
140: result.append("three ");
141: break;
142: case 2:
143: result.append("two ");
144: break;
145: case 1:
146: result.append("one ");
147: break;
148: case 0:
149: result.append("");
150: break;
151: }
152: }
153:
154: public static void main(String[] args) {
155: System.out.println(intToEnglish(Integer.parseInt(args[0])));
156: }
157:
158: }
|