RuneHive-Game
Loading...
Searching...
No Matches
Words.java
Go to the documentation of this file.
1package com.runehive.util;
2
3/**
4 * This class specially written to convert the given number into words. It will
5 * support upto 1 crore.
6 *
7 * @author SANTHOSH REDDY MANDADI
8 * @version release: 2_0_48
9 * @since April 03 2008
10 */
11public class Words {
12 long num;
13
14 private Words() {
15 num = 0;
16 }
17
18 private Words(long num) {
19 this.num = num;
20 }
21
22 public void setNumber(long num) {
23 this.num = num;
24 }
25
26 public long getNumber() {
27 return num;
28 }
29
30 public static Words getInstance(long num) {
31 return new Words(num);
32 }
33
34 public static String leftChars(String str, int n) {
35 if (str.length() <= n)
36 return str;
37 else
38 return str.substring(0, n);
39 }
40
41 public static String rightChars(String str, int n) {
42 if (str.length() <= n)
43 return str;
44 else
45 return str.substring(str.length() - n);
46 }
47
48 public long leftChars(int n) {
49 return Long.parseLong(leftChars(Long.toString(num), n));
50 }
51
52 public long rightChars(int n) {
53 return Long.parseLong(rightChars(Long.toString(num), n));
54 }
55
56 public long leftChars(long num, int n) {
57 return Long.parseLong(leftChars(Long.toString(num), n));
58 }
59
60 public long rightChars(long num, int n) {
61 return Long.parseLong(rightChars(Long.toString(num), n));
62 }
63
64 public int length(long num) {
65 return Long.toString(num).length();
66 }
67
68 private String belowTen(long x) {
69 switch ((int) x) {
70 case 1:
71 return "One ";
72 case 2:
73 return "Two ";
74 case 3:
75 return "Three ";
76 case 4:
77 return "Four ";
78 case 5:
79 return "Five ";
80 case 6:
81 return "Six ";
82 case 7:
83 return "Seven ";
84 case 8:
85 return "Eight ";
86 case 9:
87 return "Nine ";
88 }
89 return "";
90 }
91
92 private String belowTwenty(long x) {
93 if (x < 10)
94 return belowTen(x);
95 switch ((int) x) {
96 case 10:
97 return "Ten ";
98 case 11:
99 return "Eleven ";
100 case 12:
101 return "Twelve ";
102 case 13:
103 return "Thirteen ";
104 case 14:
105 return "Fourteen ";
106 case 15:
107 return "Fifteen ";
108 case 16:
109 return "Sixteen ";
110 case 17:
111 return "Seventeen ";
112 case 18:
113 return "Eighteen ";
114 case 19:
115 return "Nineteen ";
116 }
117 return "";
118 }
119
120 private String belowHundred(long x) {
121 if (x < 10)
122 return belowTen(x);
123 else if (x < 20)
124 return belowTwenty(x);
125 switch ((int) leftChars(x, 1)) {
126 case 2:
127 return "Twenty " + belowTen(rightChars(x, 1));
128 case 3:
129 return "Thirty " + belowTen(rightChars(x, 1));
130 case 4:
131 return "Fourty " + belowTen(rightChars(x, 1));
132 case 5:
133 return "Fifty " + belowTen(rightChars(x, 1));
134 case 6:
135 return "Sixty " + belowTen(rightChars(x, 1));
136 case 7:
137 return "Seventy " + belowTen(rightChars(x, 1));
138 case 8:
139 return "Eighty " + belowTen(rightChars(x, 1));
140 case 9:
141 return "Ninety " + belowTen(rightChars(x, 1));
142 }
143 return "";
144 }
145
146 private String belowThousand(long x) {
147 if (x < 10)
148 return belowTen(x);
149 else if (x < 20)
150 return belowTwenty(x);
151 else if (x < 100)
152 return belowHundred(x);
153 return belowTen(leftChars(x, 1)) + "Hundred " + belowHundred(rightChars(x, 2));
154 }
155
156 private String belowLakh(long x) {
157 if (x < 10)
158 return belowTen(x);
159 else if (x < 20)
160 return belowTwenty(x);
161 else if (x < 100)
162 return belowHundred(x);
163 else if (x < 1000)
164 return belowThousand(x);
165 if (length(x) == 4)
166 return belowTen(leftChars(x, 1)) + "Thousand " + belowThousand(rightChars(x, 3));
167 else
168 return belowHundred(leftChars(x, 2)) + "Thousand " + belowThousand(rightChars(x, 3));
169 }
170
171 public String belowCrore(long x) {
172 if (x < 10)
173 return belowTen(x);
174 else if (x < 20)
175 return belowTwenty(x);
176 else if (x < 100)
177 return belowHundred(x);
178 else if (x < 1000)
179 return belowThousand(x);
180 else if (x < 100000)
181 return belowLakh(x);
182 if (length(x) == 6)
183 return belowTen(leftChars(x, 1)) + "Lakh " + belowLakh(rightChars(x, 5));
184 else
185 return belowHundred(leftChars(x, 2)) + "Lakh " + belowLakh(rightChars(x, 5));
186 }
187
188 public String belowBillion(long x) {
189 if (x < 10)
190 return belowTen(x);
191 else if (x < 20)
192 return belowTwenty(x);
193 else if (x < 100)
194 return belowHundred(x);
195 else if (x < 1000)
196 return belowThousand(x);
197 else if (x < 100000)
198 return belowLakh(x);
199 else if (x < 100000000)
200 return belowCrore(x);
201
202 if (length(x) == 8)
203 return belowTen(leftChars(x, 1)) + "Bilion " + belowCrore(rightChars(x, 7));
204 else
205 return belowHundred(leftChars(x, 2)) + "Bilion " + belowCrore(rightChars(x, 7));
206 }
207
208 public String getNumberInWords() {
209 if (num < 10)
210 return belowTen(num);
211 else if (num < 20)
212 return belowTwenty(num);
213 else if (num < 100)
214 return belowHundred(num);
215 else if (num < 1000)
216 return belowThousand(num);
217 else if (num < 100000)
218 return belowLakh(num);
219 else if (num < 10000000)
220 return belowCrore(num);
221 else if (num < 1000000000)
222 return belowBillion(num);
223 return "";
224 }
225
226}
String belowTen(long x)
Definition Words.java:68
String belowTwenty(long x)
Definition Words.java:92
String belowThousand(long x)
Definition Words.java:146
String belowLakh(long x)
Definition Words.java:156
static String rightChars(String str, int n)
Definition Words.java:41
String belowBillion(long x)
Definition Words.java:188
String getNumberInWords()
Definition Words.java:208
String belowHundred(long x)
Definition Words.java:120
int length(long num)
Definition Words.java:64
void setNumber(long num)
Definition Words.java:22
long rightChars(long num, int n)
Definition Words.java:60
static Words getInstance(long num)
Definition Words.java:30
long leftChars(int n)
Definition Words.java:48
String belowCrore(long x)
Definition Words.java:171
long rightChars(int n)
Definition Words.java:52
static String leftChars(String str, int n)
Definition Words.java:34
long leftChars(long num, int n)
Definition Words.java:56