Skip to content

Commit 89f1c27

Browse files
authored
BAEL-1498 Money into words (eugenp#3797)
* Add tradukisto library * Implement MoneyIntoWords * Update the version of tradukisto library * Refactor MoneyIntoWords * Refactor * Refactor pom.xml * Refactor pom.xml * Refactor NumberWordConverter * BAEL-1498 Small refactoring * Test edge cases * BAEL-1498 Additional tests
1 parent c5fadb5 commit 89f1c27

3 files changed

Lines changed: 166 additions & 2 deletions

File tree

‎algorithms/pom.xml‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<exec-maven-plugin.version>1.5.0</exec-maven-plugin.version>
1010
<lombok.version>1.16.12</lombok.version>
1111
<commons-math3.version>3.6.1</commons-math3.version>
12+
<tradukisto.version>1.0.1</tradukisto.version>
1213
</properties>
1314

1415
<parent>
@@ -39,14 +40,18 @@
3940
<artifactId>jgrapht-core</artifactId>
4041
<version>1.0.1</version>
4142
</dependency>
43+
<dependency>
44+
<groupId>pl.allegro.finance</groupId>
45+
<artifactId>tradukisto</artifactId>
46+
<version>${tradukisto.version}</version>
47+
</dependency>
4248
<dependency>
4349
<groupId>org.assertj</groupId>
4450
<artifactId>assertj-core</artifactId>
4551
<version>3.9.0</version>
4652
<scope>test</scope>
4753
</dependency>
4854
</dependencies>
49-
5055
<build>
5156
<pluginManagement>
5257
<plugins>
@@ -77,4 +82,4 @@
7782
</plugin>
7883
</plugins>
7984
</reporting>
80-
</project>
85+
</project>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.baeldung.algorithms.numberwordconverter;
2+
3+
import java.math.BigDecimal;
4+
5+
import pl.allegro.finance.tradukisto.MoneyConverters;
6+
7+
public class NumberWordConverter {
8+
9+
public static final String INVALID_INPUT_GIVEN = "Invalid input given";
10+
11+
public static final String[] ones = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
12+
13+
public static final String[] tens = {
14+
"", // 0
15+
"", // 1
16+
"twenty", // 2
17+
"thirty", // 3
18+
"forty", // 4
19+
"fifty", // 5
20+
"sixty", // 6
21+
"seventy", // 7
22+
"eighty", // 8
23+
"ninety" // 9
24+
};
25+
26+
public static String getMoneyIntoWords(String input) {
27+
MoneyConverters converter = MoneyConverters.ENGLISH_BANKING_MONEY_VALUE;
28+
return converter.asWords(new BigDecimal(input));
29+
}
30+
31+
public static String getMoneyIntoWords(final double money) {
32+
long dollar = (long) money;
33+
long cents = Math.round((money - dollar) * 100);
34+
if (money == 0D) {
35+
return "";
36+
}
37+
if (money < 0) {
38+
return INVALID_INPUT_GIVEN;
39+
}
40+
String dollarPart = "";
41+
if (dollar > 0) {
42+
dollarPart = convert(dollar) + " dollar" + (dollar == 1 ? "" : "s");
43+
}
44+
String centsPart = "";
45+
if (cents > 0) {
46+
if (dollarPart.length() > 0) {
47+
centsPart = " and ";
48+
}
49+
centsPart += convert(cents) + " cent" + (cents == 1 ? "" : "s");
50+
}
51+
return dollarPart + centsPart;
52+
}
53+
54+
private static String convert(final long n) {
55+
if (n < 0) {
56+
return INVALID_INPUT_GIVEN;
57+
}
58+
if (n < 20) {
59+
return ones[(int) n];
60+
}
61+
if (n < 100) {
62+
return tens[(int) n / 10] + ((n % 10 != 0) ? " " : "") + ones[(int) n % 10];
63+
}
64+
if (n < 1000) {
65+
return ones[(int) n / 100] + " hundred" + ((n % 100 != 0) ? " " : "") + convert(n % 100);
66+
}
67+
if (n < 1_000_000) {
68+
return convert(n / 1000) + " thousand" + ((n % 1000 != 0) ? " " : "") + convert(n % 1000);
69+
}
70+
if (n < 1_000_000_000) {
71+
return convert(n / 1_000_000) + " million" + ((n % 1_000_000 != 0) ? " " : "") + convert(n % 1_000_000);
72+
}
73+
return convert(n / 1_000_000_000) + " billion" + ((n % 1_000_000_000 != 0) ? " " : "") + convert(n % 1_000_000_000);
74+
}
75+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.baeldung.algorithms.moneywords;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
import com.baeldung.algorithms.numberwordconverter.NumberWordConverter;
8+
9+
public class NumberWordConverterTest {
10+
11+
@Test
12+
public void whenMoneyNegative_thenReturnInvalidInput() {
13+
assertEquals(NumberWordConverter.INVALID_INPUT_GIVEN, NumberWordConverter.getMoneyIntoWords(-13));
14+
}
15+
16+
@Test
17+
public void whenZeroDollarsGiven_thenReturnEmptyString() {
18+
assertEquals("", NumberWordConverter.getMoneyIntoWords(0));
19+
}
20+
21+
@Test
22+
public void whenOnlyDollarsGiven_thenReturnWords() {
23+
assertEquals("one dollar", NumberWordConverter.getMoneyIntoWords(1));
24+
}
25+
26+
@Test
27+
public void whenOnlyCentsGiven_thenReturnWords() {
28+
assertEquals("sixty cents", NumberWordConverter.getMoneyIntoWords(0.6));
29+
}
30+
31+
@Test
32+
public void whenAlmostAMillioDollarsGiven_thenReturnWords() {
33+
String expectedResult = "nine hundred ninety nine thousand nine hundred ninety nine dollars";
34+
assertEquals(expectedResult, NumberWordConverter.getMoneyIntoWords(999_999));
35+
}
36+
37+
@Test
38+
public void whenThirtyMillionDollarsGiven_thenReturnWords() {
39+
String expectedResult = "thirty three million three hundred forty eight thousand nine hundred seventy eight dollars";
40+
assertEquals(expectedResult, NumberWordConverter.getMoneyIntoWords(33_348_978));
41+
}
42+
43+
@Test
44+
public void whenTwoBillionDollarsGiven_thenReturnWords() {
45+
String expectedResult = "two billion one hundred thirty three million two hundred forty seven thousand eight hundred ten dollars";
46+
assertEquals(expectedResult, NumberWordConverter.getMoneyIntoWords(2_133_247_810));
47+
}
48+
49+
@Test
50+
public void whenGivenDollarsAndCents_thenReturnWords() {
51+
String expectedResult = "nine hundred twenty four dollars and sixty cents";
52+
assertEquals(expectedResult, NumberWordConverter.getMoneyIntoWords(924.6));
53+
}
54+
55+
@Test
56+
public void whenOneDollarAndNoCents_thenReturnDollarSingular() {
57+
assertEquals("one dollar", NumberWordConverter.getMoneyIntoWords(1));
58+
}
59+
60+
@Test
61+
public void whenNoDollarsAndOneCent_thenReturnCentSingular() {
62+
assertEquals("one cent", NumberWordConverter.getMoneyIntoWords(0.01));
63+
}
64+
65+
@Test
66+
public void whenNoDollarsAndTwoCents_thenReturnCentsPlural() {
67+
assertEquals("two cents", NumberWordConverter.getMoneyIntoWords(0.02));
68+
}
69+
70+
@Test
71+
public void whenNoDollarsAndNinetyNineCents_thenReturnWords() {
72+
assertEquals("ninety nine cents", NumberWordConverter.getMoneyIntoWords(0.99));
73+
}
74+
75+
@Test
76+
public void whenNoDollarsAndNineFiveNineCents_thenCorrectRounding() {
77+
assertEquals("ninety six cents", NumberWordConverter.getMoneyIntoWords(0.959));
78+
}
79+
80+
@Test
81+
public void whenGivenDollarsAndCents_thenReturnWordsVersionTwo() {
82+
assertEquals("three hundred ten £ 00/100", NumberWordConverter.getMoneyIntoWords("310"));
83+
}
84+
}

0 commit comments

Comments
 (0)