Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LANG-1695: NumberUtils::isParseable does not recognise numbers ending in dot #1071

Closed
wants to merge 3 commits into from
Closed
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
5 changes: 1 addition & 4 deletions src/main/java/org/apache/commons/lang3/math/NumberUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1754,9 +1754,6 @@ public static boolean isParsable(final String str) {
if (StringUtils.isEmpty(str)) {
return false;
}
if (str.charAt(str.length() - 1) == '.') {
return false;
}
if (str.charAt(0) == '-') {
if (str.length() == 1) {
return false;
Expand All @@ -1776,7 +1773,7 @@ private static boolean withDecimalsParsing(final String str, final int beginIdx)
if (decimalPoints > 1) {
return false;
}
if (!isDecimalPoint && !Character.isDigit(str.charAt(i))) {
if (!isDecimalPoint && !Character.isDigit(str.charAt(i)) && !(i == str.length() - 1 && str.charAt(i) == '.')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens in countries that flip the meaning of the comma and period?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps there could be another method that takes a Locale object. Otherwise, I don't think the current parser should concern itself with it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per current Javadoc "Parsable numbers include those Strings understood by Integer#parseInt(String), Long#parseLong(String), Float#parseFloat(String)} or Double#parseDouble(String)."

If those methods don't worry about countries that flip the meaning of the comma and period, we shouldn't do it here. That would be a breaking behavior change.

As to another method that uses Locales, that's in java.util so outside the advertised scope for commons-lang.

return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,9 @@ public void testIsParsable() {
assertFalse(NumberUtils.isParsable("pendro"));
assertFalse(NumberUtils.isParsable("64, 2"));
assertFalse(NumberUtils.isParsable("64.2.2"));
assertFalse(NumberUtils.isParsable("64."));
assertTrue(NumberUtils.isParsable("64."));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a test that 64.. is false

Copy link
Contributor Author

@orionlibs orionlibs Jul 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elharo sorry. My understanding of the ticket is that 64. is true i.e. parseable and valid

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. is valid but 64.. (two periods) is not

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right. I added a test for it

assertFalse(NumberUtils.isParsable("64.."));
assertTrue(NumberUtils.isParsable("-64."));
assertFalse(NumberUtils.isParsable("64L"));
assertFalse(NumberUtils.isParsable("-"));
assertFalse(NumberUtils.isParsable("--2"));
Expand Down
Loading