-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberToWords.java
More file actions
229 lines (204 loc) · 7.23 KB
/
Copy pathNumberToWords.java
File metadata and controls
229 lines (204 loc) · 7.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
* @author genVlad
* Algorithm by: @see <a href="http://www.blackwasp.co.uk/NumberToWords.aspx">http://www.blackwasp.co.uk/NumberToWords.aspx</a>
*/
public class NumberToWords {
// Single-digit and small number names
private static String[] _smallNumbers = new String[]{"nula", "jedna", "dva", "tri", "štyri", "päť", "šesť", "sedem", "osem", "deväť", "desať", "jedenásť", "dvanásť", "trinásť", "štrnásť", "pätnásť", "šestnásť", "sedemnásť", "osemnásť", "devätnásť"};
// Tens number names from twenty upwards
private static String[] _tens = new String[]{"", "", "dvadsať", "tridsať", "štyridsať", "päťdesiat", "šesťdesiat", "sedemdesiat", "osemdesiat", "deväťdesiat"};
// Scale number names for use during recombination
private static String[] _scaleNumbers = new String[]{"", "tisíc", "milión", "miliarda"};
/**
* Converts an amount into Slovak words in eur
* @see <a href="https://jazykovaporadna.sme.sk/q/3743/">https://jazykovaporadna.sme.sk/q/3743/</a>
* @param number value to convert
* @return
*/
public static String amountToWords(double number) {
String text = "";
long[] values = split(number);
if(values[0] >= 0) {
if(values[0] == 1) {
text += "jedno euro";
}
else if(values[0] >= 2) {
text += "dve eurá";
}
else if(values[0] == 3 || values[0] == 4) {
text += numberToWords(values[0]) + "eurá";
}
else {
text += numberToWords(values[0]) + " eur";
}
}
if(values[1] > 0) {
text += " a " + numberToWords(values[1]);
String suffix;
if(values[1] == 1) {
suffix = " cent";
}
else if(values[1] >= 2 && values[1] <= 4) {
suffix = " centy";
}
else {
suffix = " centov";
}
text += suffix;
}
return text;
}
/**
* Converts an double value into Slovak words
* @see <a href="https://jazykovaporadna.sme.sk/q/704/">https://jazykovaporadna.sme.sk/q/704/</a>
* @param number value to convert
* @return
*/
public static String decimalNumberToWords(double number) {
String text = "";
long[] values = split(number);
if(values[0] == 1) {
text += "jedna";
}
else if(values[0] >= 2) {
text += "dve";
}
else {
text += numberToWords(values[0]);
}
if(values[1] > 0) {
String decimalSeparator;
if(values[0] == 1) {
decimalSeparator = " celá ";
}
else if(values[0] >= 2 && values[0] <= 4) {
decimalSeparator = " celé ";
}
else {
decimalSeparator = " celých ";
}
text += decimalSeparator + numberToWords(values[1]);
}
return text;
}
/**
* Converts an long value into Slovak words
* @param number value to convert
* @return
*/
public static String numberToWords(long number) {
// Ensure a positive number to extract from
long positive = Math.abs(number);
if(getDigitsCount(positive) > 12) {
throw new RuntimeException("Max digits count 12.");
}
// Zero rule
if (number == 0)
return _smallNumbers[0];
// Array to hold four three-digit groups
long[] digitGroups = new long[4];
// Extract the three-digit groups
for (int i = 0; i < 4; i++) {
digitGroups[i] = positive % 1000;
positive /= 1000;
}
// Convert each three-digit group to words
String[] groupText = new String[4];
for (int i = 0; i < 4; i++)
groupText[i] = threeDigitGroupToWords(digitGroups[i]);
// Recombine the three-digit groups
String combined = groupText[0];
// Process the remaining groups in turn, smallest to largest
for (int i = 1; i < 4; i++) {
// Only add non-zero items
if (digitGroups[i] != 0) {
// Build the string to add as a prefix
String prefix;
if(digitGroups[i] == 1) {
prefix = "";
}
else if(digitGroups[i] == 2 && i != 2) {
prefix = "dve";
}
else {
prefix = groupText[i];
}
if(i == 2) {
String suffix;
if(digitGroups[i] >= 2 && digitGroups[i] <= 4) {
suffix = "y";
}
else if(digitGroups[i] >= 5) {
suffix = "ov";
}
else {
suffix = "";
}
prefix += _scaleNumbers[i] + suffix;
}
else if(i == 3) {
if(digitGroups[i] >= 2 && digitGroups[i] <= 4) {
prefix += "miliardy";
}
else if(digitGroups[i] >= 5) {
prefix += "miliárd";
}
else {
prefix += _scaleNumbers[i];
}
}
else {
prefix += _scaleNumbers[i];
}
// Add the three-digit group to the combined string
combined = prefix + combined;
}
}
// Negative rule
if (number < 0)
combined = "mínus " + combined;
return combined;
}
// Converts a three-digit group into Slovak words
private static String threeDigitGroupToWords(long threeDigits) {
// Initialise the return text
String groupText = "";
// Determine the hundreds and the remainder
long hundreds = threeDigits / 100;
long tensUnits = threeDigits % 100;
// Hundreds rules
if (hundreds != 0) {
if(hundreds == 2) {
groupText += "dve";
}
else if(hundreds != 1) {
groupText += _smallNumbers[(int)hundreds];
}
groupText += "sto";
}
// Determine the tens and units
long tens = tensUnits / 10;
long units = tensUnits % 10;
// Tens rules
if (tens >= 2) {
groupText += _tens[(int)tens];
if (units != 0)
groupText += _smallNumbers[(int)units];
} else if (tensUnits != 0)
groupText += _smallNumbers[(int)tensUnits];
return groupText;
}
private static int getDigitsCount(long number) {
if(number == 0) {
return 1;
}
else {
return (int)(Math.log10(number)+1);
}
}
private static long[] split(double number) {
String doubleString = Double.toString(number);
String longStrings[]=doubleString.split("\\.");
return new long[]{Long.parseLong(longStrings[0]), Long.parseLong(longStrings[1])};
}
}