forked from tangorishi/learnJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberToWordsConverter.java
More file actions
60 lines (51 loc) · 1.92 KB
/
NumberToWordsConverter.java
File metadata and controls
60 lines (51 loc) · 1.92 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
import java.util.Scanner;
public class NumberToWordsConverter {
private static final String[] ones = {
"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"
};
private static final String[] teens = {
"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
};
private static final String[] tens = {
"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"
};
private static final String[] thousands = {
"", "thousand", "million", "billion", "trillion"
};
public static String convertToWords(long number) {
if (number == 0) {
return "zero";
}
return convertToWordsHelper(number).trim();
}
private static String convertToWordsHelper(long number) {
if (number < 10) {
return ones[(int)number];
} else if (number < 20) {
return teens[(int) (number - 10)];
} else if (number < 100) {
return tens[(int) (number / 10)] + " " + ones[(int) (number % 10)];
} else if (number < 1000) {
return ones[(int) (number / 100)] + " hundred " + convertToWordsHelper(number % 100);
} else {
int i = 0;
String words = "";
while (number > 0) {
if (number % 1000 != 0) {
words = convertToWordsHelper(number % 1000) + " " + thousands[i] + " " + words;
}
number /= 1000;
i++;
}
return words;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
long number = scanner.nextLong();
scanner.close();
String words = convertToWords(number);
System.out.println("In words: " + words);
}
}