Revision: 4434 http://vexi.svn.sourceforge.net/vexi/?rev=4434&view=rev Author: mkpg2 Date: 2012-09-14 02:41:49 +0000 (Fri, 14 Sep 2012) Log Message: ----------- Fix. Accept .123 as a valid number.
Modified Paths: -------------- trunk/org.vexi-library.value/src/main/java/org/vexi/value/Rational.java trunk/org.vexi-library.value/src/test/java/org/vexi/value/TestRational.java Modified: trunk/org.vexi-library.value/src/main/java/org/vexi/value/Rational.java =================================================================== --- trunk/org.vexi-library.value/src/main/java/org/vexi/value/Rational.java 2012-09-13 05:50:34 UTC (rev 4433) +++ trunk/org.vexi-library.value/src/main/java/org/vexi/value/Rational.java 2012-09-14 02:41:49 UTC (rev 4434) @@ -103,9 +103,9 @@ } } - static Pattern p = Pattern.compile("(-?\\d+)(?:.(\\d+)?)?0*(?:e(-?\\d+))?"); + static Pattern p = Pattern.compile("(-?\\d*)(?:.(\\d+)?)?0*(?:e(-?\\d+))?"); static public Rational tryParse(String s) { - Matcher m = p.matcher(s); + Matcher m = p.matcher(s); if (!m.matches()) { return null; } @@ -119,6 +119,8 @@ // 23.123 => 23123 if (decimal != null) { n += decimal; + }else if("".equals(n)){ + return null; } BigInteger numerator = new BigInteger(n); Modified: trunk/org.vexi-library.value/src/test/java/org/vexi/value/TestRational.java =================================================================== --- trunk/org.vexi-library.value/src/test/java/org/vexi/value/TestRational.java 2012-09-13 05:50:34 UTC (rev 4433) +++ trunk/org.vexi-library.value/src/test/java/org/vexi/value/TestRational.java 2012-09-14 02:41:49 UTC (rev 4434) @@ -8,6 +8,14 @@ assertEquals(Rational.valueOf(1230), Rational.valueOf(1234.5678).round(-1)); assertEquals(Rational.tryParse("1234.57"), Rational.valueOf(1234.5678).round(2)); } + + public void testParse() throws ValueException { + assertEquals(Rational.tryParse(".12"), Rational.tryParse("0.12")); + assertEquals(Rational.tryParse("0."), Rational.tryParse("0.0")); + assertEquals(Rational.tryParse(".0"), Rational.tryParse("0")); + assertNull(Rational.tryParse("")); + assertNull(Rational.tryParse(".")); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ Got visibility? Most devs has no idea what their production app looks like. Find out how fast your code is with AppDynamics Lite. http://ad.doubleclick.net/clk;262219671;13503038;y? http://info.appdynamics.com/FreeJavaPerformanceDownload.html _______________________________________________ Vexi-svn mailing list Vexi-svn@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/vexi-svn