-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathNumtoWords.java
More file actions
76 lines (62 loc) · 2.34 KB
/
NumtoWords.java
File metadata and controls
76 lines (62 loc) · 2.34 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
class NumtoWords
{
private static final String EMPTY = "";
private static final String[] X =
{
EMPTY, "One ", "Two ", "Three ", "Four ", "Five ", "Six ",
"Seven ", "Eight ", "Nine ", "Ten ", "Eleven ","Twelve ",
"Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ",
"Seventeen ", "Eighteen ", "Nineteen "
};
private static final String[] Y =
{
EMPTY, EMPTY, "Twenty ", "Thirty ", "Forty ", "Fifty ",
"Sixty ", "Seventy ", "Eighty ", "Ninety "
};
// Function to convert a single-digit or two-digit number into words
private static String convertToDigit(int n, String suffix)
{
// if `n` is zero
if (n == 0) {
return EMPTY;
}
// split `n` if it is more than 19
if (n > 19) {
return Y[n / 10] + X[n % 10] + suffix;
}
else {
return X[n] + suffix;
}
}
// Function to convert a given number (max 9-digits) into words
public static String convert(int n)
{
// for storing the word representation of the given number
StringBuilder res = new StringBuilder();
// add digits at ten million and hundred million place
res.append(convertToDigit((n / 1000000000) % 100, "Billion, "));
// add digits at ten million and hundred million place
res.append(convertToDigit((n / 10000000) % 100, "Crore, "));
// add digits at hundred thousand and one million place
res.append(convertToDigit(((n / 100000) % 100), "Lakh, "));
// add digits at thousand and tens thousand place
res.append(convertToDigit(((n / 1000) % 100), "Thousand "));
// add digit at hundred place
res.append(convertToDigit(((n / 100) % 10), "Hundred "));
if ((n > 100) && (n % 100 != 0)) {
res.append("and ");
}
// add digits at ones and tens place
res.append(convertToDigit((n % 100), ""));
return res.toString();
}
// Java program to convert numbers to words
public static void main(String[] args)
{
System.out.println(convert(99));
System.out.println(convert(1000));
System.out.println(convert(14632));
System.out.println(convert(997751076));
System.out.println(convert(Integer.MAX_VALUE)); // 2147483647
}
}