Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
import java.text.ParsePosition;
import java.util.Locale;
import java.util.Map;
import org.apache.fesod.common.util.MapUtils;
Expand Down Expand Up @@ -228,7 +229,12 @@ private static Number parse(String string, ExcelContentProperty contentProperty)
RoundingMode roundingMode = contentProperty.getNumberFormatProperty().getRoundingMode();
DecimalFormat decimalFormat = getCacheDecimalFormat(format, roundingMode);
decimalFormat.setParseBigDecimal(true);
return decimalFormat.parse(string);
ParsePosition position = new ParsePosition(0);
Number number = decimalFormat.parse(string, position);
if (number == null || position.getIndex() != string.length()) {
throw new ParseException("Unparseable number: \"" + string + "\"", position.getIndex());
}
return number;
}

private static DecimalFormat getCacheDecimalFormat(String format, RoundingMode roundingMode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,26 @@ void test_parseDouble_withFormat() throws ParseException {
Assertions.assertEquals(0.1234, result, 0.000001);
}

@Test
void test_parseDouble_withFormatRejectsTrailingText() {
Mockito.when(contentProperty.getNumberFormatProperty()).thenReturn(numberFormatProperty);
Mockito.when(numberFormatProperty.getFormat()).thenReturn("#,##0.00");
Mockito.when(numberFormatProperty.getRoundingMode()).thenReturn(RoundingMode.HALF_UP);

Assertions.assertThrows(ParseException.class, () -> NumberUtils.parseDouble("1,234.56abc", contentProperty));
}

@Test
void test_parseBigDecimal_withFormatAcceptsConfiguredSuffix() throws ParseException {
Mockito.when(contentProperty.getNumberFormatProperty()).thenReturn(numberFormatProperty);
Mockito.when(numberFormatProperty.getFormat()).thenReturn("#,##0.00kg");
Mockito.when(numberFormatProperty.getRoundingMode()).thenReturn(RoundingMode.HALF_UP);

BigDecimal result = NumberUtils.parseBigDecimal("1,234.56kg", contentProperty);

Assertions.assertEquals(0, new BigDecimal("1234.56").compareTo(result));
}

@Test
void test_parseLong_noFormat() throws ParseException {
Long result = NumberUtils.parseLong("123456789", null);
Expand Down
Loading