diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 0cd39eecc..32624b498 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -35,6 +35,7 @@ FastDateParser does not handle non-Gregorian calendars properly FastDateParser does not handle non-ASCII digits correctly Create StrBuilder APIs similar to String.format(String, Object...) + NumberUtils#createNumber - bad behaviour for leading "--" FastDateFormat's "z" pattern does not respect timezone of Calendar instances passed to format() Add org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS_8 StringUtils.equalsIgnoreCase doesn't check string reference equality diff --git a/src/main/java/org/apache/commons/lang3/math/NumberUtils.java b/src/main/java/org/apache/commons/lang3/math/NumberUtils.java index cca5a3f59..064f547ab 100644 --- a/src/main/java/org/apache/commons/lang3/math/NumberUtils.java +++ b/src/main/java/org/apache/commons/lang3/math/NumberUtils.java @@ -449,13 +449,6 @@ public static Number createNumber(String str) throws NumberFormatException { if (StringUtils.isBlank(str)) { throw new NumberFormatException("A blank string is not a valid number"); } - if (str.startsWith("--")) { - // this is protection for poorness in java.lang.BigDecimal. - // it accepts this as a legal value, but it does not appear - // to be in specification of class. OS X Java parses it to - // a wrong value. - return null; - } if (str.startsWith("0x") || str.startsWith("-0x") || str.startsWith("0X") || str.startsWith("-0X")) { int hexDigits = str.length() - 2; // drop 0x if (str.startsWith("-")) { // drop - @@ -721,7 +714,14 @@ public static BigDecimal createBigDecimal(String str) { // handle JDK1.3.1 bug where "" throws IndexOutOfBoundsException if (StringUtils.isBlank(str)) { throw new NumberFormatException("A blank string is not a valid number"); - } + } + if (str.trim().startsWith("--")) { + // this is protection for poorness in java.lang.BigDecimal. + // it accepts this as a legal value, but it does not appear + // to be in specification of class. OS X Java parses it to + // a wrong value. + throw new NumberFormatException(str + " is not a valid number."); + } return new BigDecimal(str); } diff --git a/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java b/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java index e66fa614a..b97289a7e 100644 --- a/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java @@ -229,6 +229,20 @@ public void testCreateNumber() { // LANG-693 assertEquals("createNumber(String) LANG-693 failed", Double.valueOf(Double.MAX_VALUE), NumberUtils .createNumber("" + Double.MAX_VALUE)); + + // LANG-822 + // ensure that the underlying negative number would create a BigDecimal + final Number bigNum = NumberUtils.createNumber("-1.1E-700F"); + assertEquals(BigDecimal.class,bigNum.getClass()); + assertNotNull(bigNum); + + // Check that the code fails to create a valid number when preceeded by -- rather than - + try { + NumberUtils.createNumber("--1.1E-700F"); + fail("Expected NumberFormatException"); + } catch (NumberFormatException nfe) { + // expected + } } @Test