LANG-1087: NumberUtils#createNumber() returns positive BigDecimal when negative Float is expected. Thanks to Renat Zhilkibaev.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1663129 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benedikt Ritter 2015-03-01 16:48:22 +00:00
parent 61836183b1
commit 45a6467088
3 changed files with 45 additions and 3 deletions

View File

@ -22,6 +22,7 @@
<body>
<release version="3.4" date="tba" description="tba">
<action issue="LANG-1087" type="fix" dev="britter" due-to="Renat Zhilkibaev">NumberUtils#createNumber() returns positive BigDecimal when negative Float is expected</action>
<action issue="LANG-1081" type="fix" dev="britter" due-to="Jonathan Baker">DiffBuilder.append(String, Object left, Object right) does not do a left.equals(right) check</action>
<action issue="LANG-1055" type="fix" dev="britter" due-to="Jonathan Baker">StrSubstitutor.replaceSystemProperties does not work consistently</action>
<action issue="LANG-1082" type="add" dev="britter" due-to="Jonathan Baker">Add option to disable the "objectsTriviallyEqual" test in DiffBuilder</action>

View File

@ -503,16 +503,16 @@ public class NumberUtils {
} else {
dec = str.substring(decPos + 1);
}
mant = str.substring(0, decPos);
mant = getMantissa(str, decPos);
numDecimals = dec.length(); // gets number of digits past the decimal to ensure no loss of precision for floating point numbers.
} else {
if (expPos > -1) {
if (expPos > str.length()) { // prevents double exponent causing IOOBE
throw new NumberFormatException(str + " is not a valid number.");
}
mant = str.substring(0, expPos);
mant = getMantissa(str, expPos);
} else {
mant = str;
mant = getMantissa(str);
}
dec = null;
}
@ -623,6 +623,34 @@ public class NumberUtils {
return createBigDecimal(str);
}
/**
* <p>Utility method for {@link #createNumber(java.lang.String)}.</p>
*
* <p>Returns mantissa of the given number.</p>
*
* @param str the string representation of the number
* @return mantissa of the given number
*/
private static String getMantissa(final String str) {
return getMantissa(str, str.length());
}
/**
* <p>Utility method for {@link #createNumber(java.lang.String)}.</p>
*
* <p>Returns mantissa of the given number.</p>
*
* @param str the string representation of the number
* @param stopPos the position of the exponent or decimal point
* @return mantissa of the given number
*/
private static String getMantissa(final String str, final int stopPos) {
final char firstChar = str.charAt(0);
final boolean hasSign = (firstChar == '-' || firstChar == '+');
return hasSign ? str.substring(1, stopPos) : str.substring(0, stopPos);
}
/**
* <p>Utility method for {@link #createNumber(java.lang.String)}.</p>
*

View File

@ -245,6 +245,19 @@ public class NumberUtilsTest {
assertNotNull(bigNum);
assertEquals(BigDecimal.class, bigNum.getClass());
}
@Test
public void testLang1087(){
// no sign cases
assertEquals(Float.class, NumberUtils.createNumber("0.0").getClass());
assertEquals(Float.valueOf("0.0"), NumberUtils.createNumber("0.0"));
// explicit positive sign cases
assertEquals(Float.class, NumberUtils.createNumber("+0.0").getClass());
assertEquals(Float.valueOf("+0.0"), NumberUtils.createNumber("+0.0"));
// negative sign cases
assertEquals(Float.class, NumberUtils.createNumber("-0.0").getClass());
assertEquals(Float.valueOf("-0.0"), NumberUtils.createNumber("-0.0"));
}
@Test
public void TestLang747() {