Skip to content

Commit 05e4337

Browse files
Added a new feature to convert numbers into their word representation
1 parent f9d0508 commit 05e4337

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.thealgorithms.conversions;
2+
3+
import java.math.BigDecimal;
4+
5+
/**
6+
A Java-based utility for converting numeric values into their English word
7+
representations. Whether you need to convert a small number, a large number
8+
with millions and billions, or even a number with decimal places, this utility
9+
has you covered.
10+
*
11+
*/
12+
public class NumberToWords {
13+
14+
private NumberToWords() {
15+
}
16+
17+
private static final String[] units = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
18+
19+
private static final String[] tens = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
20+
21+
private static final String[] powers = {"", "Thousand", "Million", "Billion", "Trillion"};
22+
23+
private static final String ZERO = "Zero";
24+
private static final String POINT = " Point";
25+
private static final String NEGATIVE = "Negative ";
26+
27+
public static String convert(BigDecimal number) {
28+
if (number == null) {
29+
return "Invalid Input";
30+
}
31+
32+
// Check for negative sign
33+
boolean isNegative = number.signum() < 0;
34+
35+
// Split the number into whole and fractional parts
36+
BigDecimal[] parts = number.abs().divideAndRemainder(BigDecimal.ONE);
37+
BigDecimal wholePart = parts[0]; // Keep whole part as BigDecimal
38+
String fractionalPartStr = parts[1].compareTo(BigDecimal.ZERO) > 0 ? parts[1].toPlainString().substring(2) : ""; // Get fractional part only if it exists
39+
40+
// Convert whole part to words
41+
StringBuilder result = new StringBuilder();
42+
if (isNegative) {
43+
result.append(NEGATIVE);
44+
}
45+
result.append(convertWholeNumberToWords(wholePart));
46+
47+
// Convert fractional part to words
48+
if (!fractionalPartStr.isEmpty()) {
49+
result.append(POINT);
50+
for (char digit : fractionalPartStr.toCharArray()) {
51+
int digitValue = Character.getNumericValue(digit);
52+
result.append(" ").append(digitValue == 0 ? ZERO : units[digitValue]);
53+
}
54+
}
55+
56+
return result.toString().trim();
57+
}
58+
59+
private static String convertWholeNumberToWords(BigDecimal number) {
60+
if (number.compareTo(BigDecimal.ZERO) == 0) {
61+
return ZERO;
62+
}
63+
64+
StringBuilder words = new StringBuilder();
65+
int power = 0;
66+
67+
while (number.compareTo(BigDecimal.ZERO) > 0) {
68+
// Get the last three digits
69+
BigDecimal[] divisionResult = number.divideAndRemainder(BigDecimal.valueOf(1000));
70+
int chunk = divisionResult[1].intValue();
71+
72+
if (chunk > 0) {
73+
String chunkWords = convertChunk(chunk);
74+
if (power > 0) {
75+
words.insert(0, powers[power] + " ");
76+
}
77+
words.insert(0, chunkWords + " ");
78+
}
79+
80+
number = divisionResult[0]; // Continue with the remaining part
81+
power++;
82+
}
83+
84+
return words.toString().trim();
85+
}
86+
87+
private static String convertChunk(int number) {
88+
String chunkWords;
89+
90+
if (number < 20) {
91+
chunkWords = units[number];
92+
} else if (number < 100) {
93+
chunkWords = tens[number / 10] + (number % 10 > 0 ? " " + units[number % 10] : "");
94+
} else {
95+
chunkWords = units[number / 100] + " Hundred" + (number % 100 > 0 ? " " + convertChunk(number % 100) : "");
96+
}
97+
98+
return chunkWords;
99+
}
100+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.thealgorithms.conversions;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.math.BigDecimal;
6+
import org.junit.jupiter.api.Test;
7+
8+
public class NumberToWordsTest {
9+
10+
@Test
11+
void testNullInput() {
12+
assertEquals("Invalid Input", NumberToWords.convert(null), "Null input should return 'Invalid Input'");
13+
}
14+
15+
@Test
16+
void testZeroInput() {
17+
assertEquals("Zero", NumberToWords.convert(BigDecimal.ZERO), "Zero input should return 'Zero'");
18+
}
19+
20+
@Test
21+
void testPositiveWholeNumbers() {
22+
assertEquals("One", NumberToWords.convert(BigDecimal.ONE), "1 should convert to 'One'");
23+
assertEquals("One Thousand", NumberToWords.convert(new BigDecimal("1000")), "1000 should convert to 'One Thousand'");
24+
assertEquals("One Million", NumberToWords.convert(new BigDecimal("1000000")), "1000000 should convert to 'One Million'");
25+
}
26+
27+
@Test
28+
void testNegativeWholeNumbers() {
29+
assertEquals("Negative One", NumberToWords.convert(new BigDecimal("-1")), "-1 should convert to 'Negative One'");
30+
assertEquals("Negative One Thousand", NumberToWords.convert(new BigDecimal("-1000")), "-1000 should convert to 'Negative One Thousand'");
31+
}
32+
33+
@Test
34+
void testFractionalNumbers() {
35+
assertEquals("Zero Point One Two Three", NumberToWords.convert(new BigDecimal("0.123")), "0.123 should convert to 'Zero Point One Two Three'");
36+
assertEquals("Negative Zero Point Four Five Six", NumberToWords.convert(new BigDecimal("-0.456")), "-0.456 should convert to 'Negative Zero Point Four Five Six'");
37+
}
38+
39+
@Test
40+
void testLargeNumbers() {
41+
assertEquals("Nine Hundred Ninety Nine Million Nine Hundred Ninety Nine Thousand Nine Hundred Ninety Nine", NumberToWords.convert(new BigDecimal("999999999")), "999999999 should convert correctly");
42+
assertEquals("One Trillion", NumberToWords.convert(new BigDecimal("1000000000000")), "1000000000000 should convert to 'One Trillion'");
43+
}
44+
45+
@Test
46+
void testNegativeLargeNumbers() {
47+
assertEquals("Negative Nine Trillion Eight Hundred Seventy Six Billion Five Hundred Forty Three Million Two Hundred Ten Thousand Nine Hundred Eighty Seven", NumberToWords.convert(new BigDecimal("-9876543210987")), "-9876543210987 should convert correctly");
48+
}
49+
50+
@Test
51+
void testFloatingPointPrecision() {
52+
assertEquals("One Million Point Zero Zero One", NumberToWords.convert(new BigDecimal("1000000.001")), "1000000.001 should convert to 'One Million Point Zero Zero One'");
53+
}
54+
55+
@Test
56+
void testEdgeCases() {
57+
assertEquals("Negative Zero", NumberToWords.convert(new BigDecimal("-0.0")), "-0.0 should convert to 'Negative Zero'");
58+
assertEquals("Zero Point Zero Zero Zero Zero Zero Zero One", NumberToWords.convert(new BigDecimal("1E-7")), "1E-7 should convert to 'Zero Point Zero Zero Zero Zero Zero Zero One'");
59+
}
60+
}

0 commit comments

Comments
 (0)