From 6240a05c12914083adbe8c7c53e7e40ef42931dd Mon Sep 17 00:00:00 2001 From: Benedikt Ritter Date: Sat, 12 Jul 2014 09:48:13 +0000 Subject: [PATCH] =?UTF-8?q?LANG-1016:=20NumberUtils#isParsable=20method(s)?= =?UTF-8?q?.=20Apply=20Juan=20PabloSantos=20Rodr=C3=ADguez=20patch=20for?= =?UTF-8?q?=20handling=20negative=20numbers.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1609898 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/commons/lang3/math/NumberUtils.java | 6 +++++- .../org/apache/commons/lang3/math/NumberUtilsTest.java | 7 ++++++- 2 files changed, 11 insertions(+), 2 deletions(-) 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 2cbb975e5..c00c5cf28 100644 --- a/src/main/java/org/apache/commons/lang3/math/NumberUtils.java +++ b/src/main/java/org/apache/commons/lang3/math/NumberUtils.java @@ -1488,7 +1488,11 @@ public class NumberUtils { if( StringUtils.endsWith( str, "." ) ) { return false; } - return isDigits( StringUtils.replaceOnce( str, ".", StringUtils.EMPTY ) ); + if( StringUtils.startsWith( str, "-" ) ) { + return isDigits( StringUtils.replaceOnce( StringUtils.substring( str, 1 ), ".", StringUtils.EMPTY ) ); + } else { + return isDigits( StringUtils.replaceOnce( str, ".", StringUtils.EMPTY ) ); + } } } 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 96b7a7334..e9ec47da8 100644 --- a/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java @@ -1268,7 +1268,12 @@ public class NumberUtilsTest { assertFalse( NumberUtils.isParsable("64L") ); assertTrue( NumberUtils.isParsable("64.2") ); assertTrue( NumberUtils.isParsable("64") ); - assertTrue(NumberUtils.isParsable("018")); + assertTrue( NumberUtils.isParsable("018") ); + assertTrue( NumberUtils.isParsable(".18") ); + assertTrue( NumberUtils.isParsable("-65") ); + assertTrue( NumberUtils.isParsable("-018") ); + assertTrue( NumberUtils.isParsable("-018.2") ); + assertTrue( NumberUtils.isParsable("-.236") ); } private boolean checkCreateNumber(final String val) {