commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brit...@apache.org
Subject svn commit: r1663129 - in /commons/proper/lang/trunk/src: changes/changes.xml main/java/org/apache/commons/lang3/math/NumberUtils.java test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
Date Sun, 01 Mar 2015 16:48:23 GMT
Author: britter
Date: Sun Mar  1 16:48:22 2015
New Revision: 1663129

URL: http://svn.apache.org/r1663129
Log:
LANG-1087: NumberUtils#createNumber() returns positive BigDecimal when negative Float is expected.
Thanks to Renat Zhilkibaev.

Modified:
    commons/proper/lang/trunk/src/changes/changes.xml
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java

Modified: commons/proper/lang/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/changes/changes.xml?rev=1663129&r1=1663128&r2=1663129&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/changes/changes.xml [utf-8] (original)
+++ commons/proper/lang/trunk/src/changes/changes.xml [utf-8] Sun Mar  1 16:48:22 2015
@@ -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>

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java?rev=1663129&r1=1663128&r2=1663129&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
(original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
Sun Mar  1 16:48:22 2015
@@ -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;
         }
@@ -624,6 +624,34 @@ public class NumberUtils {
     }
 
     /**
+     * <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>
      *
      * <p>Returns <code>true</code> if s is <code>null</code>.</p>

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java?rev=1663129&r1=1663128&r2=1663129&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
(original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
Sun Mar  1 16:48:22 2015
@@ -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() {



Mime
View raw message