Repository: jtablesaw/tablesaw
Pull request #1133 (2022-08-05 09:13:25)

Double Parser Issue Fix (#1132)
Thanks for contributing.

- [x] Tick to sign-off your agreement to the [Developer Certificate of Origin (DCO) 1.1](https://developercertificate.org)

## Description

What was changed

## Testing

Did you add a unit test?
- Yes, appended to the `tech.tablesaw.api.DoubleParserTest` class


gs-asrivastav: Fixes issue https://github.com/jtablesaw/tablesaw/issues/1132

Patch:
diff --git a/core/pom.xml b/core/pom.xml
index 25e3d969..2f91c8dc 100644
--- a/core/pom.xml
+++ b/core/pom.xml
@@ -101,7 +101,7 @@
         <dependency>
             <groupId>com.ibm.icu</groupId>
             <artifactId>icu4j</artifactId>
-            <version>65.1</version>
+            <version>68.2</version>
         </dependency>
         <dependency>
             <groupId>io.github.classgraph</groupId>
diff --git a/core/src/main/java/tech/tablesaw/columns/numbers/DoubleParser.java b/core/src/main/java/tech/tablesaw/columns/numbers/DoubleParser.java
index e8264c83..49f1a07f 100644
--- a/core/src/main/java/tech/tablesaw/columns/numbers/DoubleParser.java
+++ b/core/src/main/java/tech/tablesaw/columns/numbers/DoubleParser.java
@@ -1,12 +1,13 @@
 package tech.tablesaw.columns.numbers;
 
 import com.google.common.collect.Lists;
-import java.text.NumberFormat;
-import java.text.ParseException;
 import tech.tablesaw.api.ColumnType;
 import tech.tablesaw.columns.AbstractColumnParser;
 import tech.tablesaw.io.ReadOptions;
 
+import java.text.NumberFormat;
+import java.text.ParseException;
+
 public class DoubleParser extends AbstractColumnParser<Double> {
 
   public DoubleParser(ColumnType columnType) {
@@ -56,7 +57,26 @@ public class DoubleParser extends AbstractColumnParser<Double> {
     return Double.parseDouble(AbstractColumnParser.remove(s, ','));
   }
 
+  /**
+   * A number is percent when it ends with %
+   * - We can trim off the last occurrence of '%'
+   * - The remaining string should then be parsable as a number(double)
+   *
+   * @param s Value
+   * @return Flag returning whether the input is a percent or not
+   */
   private boolean isPercent(String s) {
-    return s.charAt(s.length() - 1) == '%';
+    boolean containsPercentSymbol = s.charAt(s.length() - 1) == '%';
+    if (containsPercentSymbol) {
+      String percentageValue = s.substring(0, s.length() - 1);
+      try {
+        double value = Double.parseDouble(percentageValue);
+        return !Double.isNaN(value);
+      } catch (NumberFormatException e) {
+        // it's all part of the plan
+        return false;
+      }
+    }
+    return false;
   }
 }


Test patch:
diff --git a/core/src/test/java/tech/tablesaw/api/DoubleParserTest.java b/core/src/test/java/tech/tablesaw/api/DoubleParserTest.java
index d9fb1fa4..7273c0ff 100644
--- a/core/src/test/java/tech/tablesaw/api/DoubleParserTest.java
+++ b/core/src/test/java/tech/tablesaw/api/DoubleParserTest.java
@@ -44,6 +44,12 @@ class DoubleParserTest {
     assertFalse(doubleParser.canParse(","));
   }
 
+  @Test
+  void testCanParse5() {
+    DoubleParser doubleParser = new DoubleParser(ColumnType.DOUBLE);
+    assertFalse(doubleParser.canParse("0% - 25%"));
+  }
+
   @Test
   void testParseDouble1() {
     DoubleParser doubleParser = new DoubleParser(ColumnType.DOUBLE);