Add convertion of numbers into their word representation (#6137)

This commit is contained in:
Prathamesh Zingade
2025-01-16 13:16:57 +05:30
committed by GitHub
parent 754bf6c5f8
commit 466ff0b4c2
3 changed files with 190 additions and 18 deletions

View File

@@ -0,0 +1,100 @@
package com.thealgorithms.conversions;
import java.math.BigDecimal;
/**
A Java-based utility for converting numeric values into their English word
representations. Whether you need to convert a small number, a large number
with millions and billions, or even a number with decimal places, this utility
has you covered.
*
*/
public final class NumberToWords {
private NumberToWords() {
}
private static final String[] UNITS = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "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[] POWERS = {"", "Thousand", "Million", "Billion", "Trillion"};
private static final String ZERO = "Zero";
private static final String POINT = " Point";
private static final String NEGATIVE = "Negative ";
public static String convert(BigDecimal number) {
if (number == null) {
return "Invalid Input";
}
// Check for negative sign
boolean isNegative = number.signum() < 0;
// Split the number into whole and fractional parts
BigDecimal[] parts = number.abs().divideAndRemainder(BigDecimal.ONE);
BigDecimal wholePart = parts[0]; // Keep whole part as BigDecimal
String fractionalPartStr = parts[1].compareTo(BigDecimal.ZERO) > 0 ? parts[1].toPlainString().substring(2) : ""; // Get fractional part only if it exists
// Convert whole part to words
StringBuilder result = new StringBuilder();
if (isNegative) {
result.append(NEGATIVE);
}
result.append(convertWholeNumberToWords(wholePart));
// Convert fractional part to words
if (!fractionalPartStr.isEmpty()) {
result.append(POINT);
for (char digit : fractionalPartStr.toCharArray()) {
int digitValue = Character.getNumericValue(digit);
result.append(" ").append(digitValue == 0 ? ZERO : UNITS[digitValue]);
}
}
return result.toString().trim();
}
private static String convertWholeNumberToWords(BigDecimal number) {
if (number.compareTo(BigDecimal.ZERO) == 0) {
return ZERO;
}
StringBuilder words = new StringBuilder();
int power = 0;
while (number.compareTo(BigDecimal.ZERO) > 0) {
// Get the last three digits
BigDecimal[] divisionResult = number.divideAndRemainder(BigDecimal.valueOf(1000));
int chunk = divisionResult[1].intValue();
if (chunk > 0) {
String chunkWords = convertChunk(chunk);
if (power > 0) {
words.insert(0, POWERS[power] + " ");
}
words.insert(0, chunkWords + " ");
}
number = divisionResult[0]; // Continue with the remaining part
power++;
}
return words.toString().trim();
}
private static String convertChunk(int number) {
String chunkWords;
if (number < 20) {
chunkWords = UNITS[number];
} else if (number < 100) {
chunkWords = TENS[number / 10] + (number % 10 > 0 ? " " + UNITS[number % 10] : "");
} else {
chunkWords = UNITS[number / 100] + " Hundred" + (number % 100 > 0 ? " " + convertChunk(number % 100) : "");
}
return chunkWords;
}
}

View File

@@ -0,0 +1,60 @@
package com.thealgorithms.conversions;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.math.BigDecimal;
import org.junit.jupiter.api.Test;
public class NumberToWordsTest {
@Test
void testNullInput() {
assertEquals("Invalid Input", NumberToWords.convert(null), "Null input should return 'Invalid Input'");
}
@Test
void testZeroInput() {
assertEquals("Zero", NumberToWords.convert(BigDecimal.ZERO), "Zero input should return 'Zero'");
}
@Test
void testPositiveWholeNumbers() {
assertEquals("One", NumberToWords.convert(BigDecimal.ONE), "1 should convert to 'One'");
assertEquals("One Thousand", NumberToWords.convert(new BigDecimal("1000")), "1000 should convert to 'One Thousand'");
assertEquals("One Million", NumberToWords.convert(new BigDecimal("1000000")), "1000000 should convert to 'One Million'");
}
@Test
void testNegativeWholeNumbers() {
assertEquals("Negative One", NumberToWords.convert(new BigDecimal("-1")), "-1 should convert to 'Negative One'");
assertEquals("Negative One Thousand", NumberToWords.convert(new BigDecimal("-1000")), "-1000 should convert to 'Negative One Thousand'");
}
@Test
void testFractionalNumbers() {
assertEquals("Zero Point One Two Three", NumberToWords.convert(new BigDecimal("0.123")), "0.123 should convert to 'Zero Point One Two Three'");
assertEquals("Negative Zero Point Four Five Six", NumberToWords.convert(new BigDecimal("-0.456")), "-0.456 should convert to 'Negative Zero Point Four Five Six'");
}
@Test
void testLargeNumbers() {
assertEquals("Nine Hundred Ninety Nine Million Nine Hundred Ninety Nine Thousand Nine Hundred Ninety Nine", NumberToWords.convert(new BigDecimal("999999999")), "999999999 should convert correctly");
assertEquals("One Trillion", NumberToWords.convert(new BigDecimal("1000000000000")), "1000000000000 should convert to 'One Trillion'");
}
@Test
void testNegativeLargeNumbers() {
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");
}
@Test
void testFloatingPointPrecision() {
assertEquals("One Million Point Zero Zero One", NumberToWords.convert(new BigDecimal("1000000.001")), "1000000.001 should convert to 'One Million Point Zero Zero One'");
}
@Test
void testEdgeCases() {
assertEquals("Zero", NumberToWords.convert(new BigDecimal("-0.0")), "-0.0 should convert to 'Zero'");
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'");
}
}