LANG-1205: NumberUtils.createNumber() behaves inconsistently with NumberUtils.isNumber()

This closes github #87 thanks to pbrose
This commit is contained in:
Chas Honton 2016-04-23 20:33:50 -07:00
parent 5e62bf80f3
commit b877fb9abe
3 changed files with 27 additions and 2 deletions

View File

@ -22,6 +22,7 @@
<body> <body>
<release version="3.5" date="tba" description="tba"> <release version="3.5" date="tba" description="tba">
<action issue="LANG-1205" type="fix" dev="chas" due-to="pbrose">NumberUtils.createNumber() behaves inconsistently with NumberUtils.isNumber()</action>
<action issue="LANG-1115" type="add" dev="chas" due-to="Jim Lloyd, Joe Ferner">Add support for varargs in ConstructorUtils, MemberUtils, and MethodUtils</action> <action issue="LANG-1115" type="add" dev="chas" due-to="Jim Lloyd, Joe Ferner">Add support for varargs in ConstructorUtils, MemberUtils, and MethodUtils</action>
<action issue="LANG-1134" type="add" dev="chas" due-to="Alan Smithee">New methods for lang3.Validate</action> <action issue="LANG-1134" type="add" dev="chas" due-to="Alan Smithee">New methods for lang3.Validate</action>
<action issue="LANG-1222" type="fix" dev="ggregory" due-to="Adam J.">Fix for incorrect comment on StringUtils.containsIgnoreCase method</action> <action issue="LANG-1222" type="fix" dev="ggregory" due-to="Adam J.">Fix for incorrect comment on StringUtils.containsIgnoreCase method</action>

View File

@ -542,7 +542,7 @@ public static Number createNumber(final String str) throws NumberFormatException
case 'f' : case 'f' :
case 'F' : case 'F' :
try { try {
final Float f = NumberUtils.createFloat(numeric); final Float f = NumberUtils.createFloat(str);
if (!(f.isInfinite() || (f.floatValue() == 0.0F && !allZeros))) { if (!(f.isInfinite() || (f.floatValue() == 0.0F && !allZeros))) {
//If it's too big for a float or the float value = 0 and the string //If it's too big for a float or the float value = 0 and the string
//has non-zeros in it, then float does not have the precision we want //has non-zeros in it, then float does not have the precision we want
@ -556,7 +556,7 @@ public static Number createNumber(final String str) throws NumberFormatException
case 'd' : case 'd' :
case 'D' : case 'D' :
try { try {
final Double d = NumberUtils.createDouble(numeric); final Double d = NumberUtils.createDouble(str);
if (!(d.isInfinite() || (d.floatValue() == 0.0D && !allZeros))) { if (!(d.isInfinite() || (d.floatValue() == 0.0D && !allZeros))) {
return d; return d;
} }

View File

@ -318,6 +318,30 @@ public void testCreateNumberFailure_4() {
NumberUtils.createNumber("1eE+00001"); NumberUtils.createNumber("1eE+00001");
} }
@Test(expected=NumberFormatException.class)
// Check that the code fails to create a valid number when there are multiple trailing 'f' characters (LANG-1205)
public void testCreateNumberFailure_5() {
NumberUtils.createNumber("1234.5ff");
}
@Test(expected=NumberFormatException.class)
// Check that the code fails to create a valid number when there are multiple trailing 'F' characters (LANG-1205)
public void testCreateNumberFailure_6() {
NumberUtils.createNumber("1234.5FF");
}
@Test(expected=NumberFormatException.class)
// Check that the code fails to create a valid number when there are multiple trailing 'd' characters (LANG-1205)
public void testCreateNumberFailure_7() {
NumberUtils.createNumber("1234.5dd");
}
@Test(expected=NumberFormatException.class)
// Check that the code fails to create a valid number when there are multiple trailing 'D' characters (LANG-1205)
public void testCreateNumberFailure_8() {
NumberUtils.createNumber("1234.5DD");
}
// Tests to show when magnitude causes switch to next Number type // Tests to show when magnitude causes switch to next Number type
// Will probably need to be adjusted if code is changed to check precision (LANG-693) // Will probably need to be adjusted if code is changed to check precision (LANG-693)
@Test @Test