Skip to content

Commit 95bafad

Browse files
authored
BAEL-9506: Convert a String Number Value with a Dollar Sign to BigDec… (#18923)
* BAEL-9506: Convert a String Number Value with a Dollar Sign to BigDecimal in Java * BAEL-9506: Convert a String Number Value with a Dollar Sign to BigDecimal in Java
1 parent 06a89a8 commit 95bafad

File tree

1 file changed

+90
-62
lines changed

1 file changed

+90
-62
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,102 @@
11
package com.baeldung.stringtobigdecimal;
22

3-
import static org.junit.Assert.assertEquals;
3+
import org.junit.Test;
44

55
import java.math.BigDecimal;
66
import java.text.DecimalFormat;
77
import java.text.DecimalFormatSymbols;
8+
import java.text.NumberFormat;
89
import java.text.ParseException;
10+
import java.util.Locale;
911

10-
import org.junit.Test;
12+
import static org.junit.Assert.assertEquals;
1113

1214
public class StringToBigDecimalConversionUnitTest {
1315

14-
@Test
15-
public void givenValidString_WhenBigDecimalObjectWithStringParameter_ThenResultIsDecimalObject() {
16-
BigDecimal bigDecimal = new BigDecimal("123");
17-
assertEquals(new BigDecimal(123), bigDecimal);
18-
}
19-
20-
@Test(expected = NullPointerException.class)
21-
public void givenNullString_WhenBigDecimalObjectWithStringParameter_ThenNullPointerExceptionIsThrown() {
22-
String bigDecimal = null;
23-
new BigDecimal(bigDecimal);
24-
}
25-
26-
@Test(expected = NumberFormatException.class)
27-
public void givenInalidString_WhenBigDecimalObjectWithStringParameter_ThenNumberFormatExceptionIsThrown() {
28-
new BigDecimal("&");
29-
}
30-
31-
@Test
32-
public void givenValidString_WhenValueOfDoubleFromString_ThenResultIsDecimalObject() {
33-
BigDecimal bigDecimal = BigDecimal.valueOf(Double.valueOf("123.42"));
34-
assertEquals(new BigDecimal(123.42).setScale(2, BigDecimal.ROUND_HALF_UP), bigDecimal);
35-
}
36-
37-
@Test(expected = NullPointerException.class)
38-
public void givenNullString_WhenValueOfDoubleFromString_ThenNullPointerExceptionIsThrown() {
39-
BigDecimal.valueOf(Double.valueOf(null));
40-
}
41-
42-
@Test(expected = NumberFormatException.class)
43-
public void givenInalidString_WhenValueOfDoubleFromString_ThenNumberFormatExceptionIsThrown() {
44-
BigDecimal.valueOf(Double.valueOf("&"));
45-
}
46-
47-
@Test
48-
public void givenValidString_WhenDecimalFormatOfString_ThenResultIsDecimalObject() throws ParseException {
49-
BigDecimal bigDecimal = new BigDecimal(10692467440017.111).setScale(3, BigDecimal.ROUND_HALF_UP);
50-
51-
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
52-
symbols.setGroupingSeparator(',');
53-
symbols.setDecimalSeparator('.');
54-
String pattern = "#,##0.0#";
55-
DecimalFormat decimalFormat = new DecimalFormat(pattern, symbols);
56-
decimalFormat.setParseBigDecimal(true);
57-
58-
// parse the string value
59-
BigDecimal parsedStringValue = (BigDecimal) decimalFormat.parse("10,692,467,440,017.111");
60-
61-
assertEquals(bigDecimal, parsedStringValue);
62-
}
63-
64-
@Test(expected = NullPointerException.class)
65-
public void givenNullString_WhenDecimalFormatOfString_ThenNullPointerExceptionIsThrown() throws ParseException {
66-
new DecimalFormat("#").parse(null);
67-
}
68-
69-
@Test(expected = ParseException.class)
70-
public void givenInalidString_WhenDecimalFormatOfString_ThenNumberFormatExceptionIsThrown() throws ParseException {
71-
new DecimalFormat("#").parse("&");
72-
}
73-
16+
@Test
17+
public void givenValidString_WhenBigDecimalObjectWithStringParameter_ThenResultIsDecimalObject() {
18+
BigDecimal bigDecimal = new BigDecimal("123");
19+
assertEquals(new BigDecimal(123), bigDecimal);
20+
}
21+
22+
@Test(expected = NullPointerException.class)
23+
public void givenNullString_WhenBigDecimalObjectWithStringParameter_ThenNullPointerExceptionIsThrown() {
24+
String bigDecimal = null;
25+
new BigDecimal(bigDecimal);
26+
}
27+
28+
@Test(expected = NumberFormatException.class)
29+
public void givenInalidString_WhenBigDecimalObjectWithStringParameter_ThenNumberFormatExceptionIsThrown() {
30+
new BigDecimal("&");
31+
}
32+
33+
@Test
34+
public void givenValidString_WhenValueOfDoubleFromString_ThenResultIsDecimalObject() {
35+
BigDecimal bigDecimal = BigDecimal.valueOf(Double.valueOf("123.42"));
36+
assertEquals(new BigDecimal(123.42).setScale(2, BigDecimal.ROUND_HALF_UP), bigDecimal);
37+
}
38+
39+
@Test(expected = NullPointerException.class)
40+
public void givenNullString_WhenValueOfDoubleFromString_ThenNullPointerExceptionIsThrown() {
41+
BigDecimal.valueOf(Double.valueOf(null));
42+
}
43+
44+
@Test(expected = NumberFormatException.class)
45+
public void givenInalidString_WhenValueOfDoubleFromString_ThenNumberFormatExceptionIsThrown() {
46+
BigDecimal.valueOf(Double.valueOf("&"));
47+
}
48+
49+
@Test
50+
public void givenValidString_WhenDecimalFormatOfString_ThenResultIsDecimalObject() throws ParseException {
51+
BigDecimal bigDecimal = new BigDecimal(10692467440017.111).setScale(3, BigDecimal.ROUND_HALF_UP);
52+
53+
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
54+
symbols.setGroupingSeparator(',');
55+
symbols.setDecimalSeparator('.');
56+
String pattern = "#,##0.0#";
57+
DecimalFormat decimalFormat = new DecimalFormat(pattern, symbols);
58+
decimalFormat.setParseBigDecimal(true);
59+
60+
// parse the string value
61+
BigDecimal parsedStringValue = (BigDecimal) decimalFormat.parse("10,692,467,440,017.111");
62+
63+
assertEquals(bigDecimal, parsedStringValue);
64+
}
65+
66+
@Test(expected = NullPointerException.class)
67+
public void givenNullString_WhenDecimalFormatOfString_ThenNullPointerExceptionIsThrown() throws ParseException {
68+
new DecimalFormat("#").parse(null);
69+
}
70+
71+
@Test(expected = ParseException.class)
72+
public void givenInalidString_WhenDecimalFormatOfString_ThenNumberFormatExceptionIsThrown() throws ParseException {
73+
new DecimalFormat("#").parse("&");
74+
}
75+
76+
@Test
77+
public void givenStringWithDollarSign_whenRemovedAndConvertedToBigDecimal_thenReturnExpectedValue() {
78+
String salePrice = "$348.45";
79+
String price = salePrice.replace("$", "");
80+
BigDecimal amount = new BigDecimal(price);
81+
82+
assertEquals(new BigDecimal("348.45"), amount);
83+
}
84+
85+
@Test
86+
public void givenStringWithDollarSign_whenParsedWithNumberFormat_thenReturnExpectedValue() throws ParseException {
87+
String salePrice = "$348.45";
88+
Locale locale = Locale.US;
89+
Number number = NumberFormat.getCurrencyInstance(locale).parse(salePrice);
90+
BigDecimal amount = new BigDecimal(number.toString());
91+
assertEquals(new BigDecimal("348.45"), amount);
92+
}
93+
94+
@Test
95+
public void givenEuroCurrencyString_whenParsedWithNumberFormat_thenReturnExpectedValue() throws ParseException {
96+
String salePrice = "348,45\u00A0€";
97+
Locale locale = Locale.FRANCE;
98+
Number number = NumberFormat.getCurrencyInstance(locale).parse(salePrice);
99+
BigDecimal amount = new BigDecimal(number.toString());
100+
assertEquals(new BigDecimal("348.45"), amount);
101+
}
74102
}

0 commit comments

Comments
 (0)