]> source.dussan.org Git - poi.git/commitdiff
Bug 54723: Support for percentage in VALUE() function
authorCédric Walter <cedricwalter@apache.org>
Fri, 16 Aug 2013 17:15:47 +0000 (17:15 +0000)
committerCédric Walter <cedricwalter@apache.org>
Fri, 16 Aug 2013 17:15:47 +0000 (17:15 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1514799 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/ss/formula/functions/Value.java
src/testcases/org/apache/poi/ss/formula/functions/TestValue.java

index 811db9c14ca3ccd81f47cb8e3878140345ee6fd0..d8bc377f97d0f11af6486b101f55f12dad6c6580 100644 (file)
@@ -34,6 +34,7 @@ import org.apache.poi.ss.formula.eval.ValueEval;
  * properly the result is <b>#VALUE!</b> error. Blank string converts to zero.
  *
  * @author Josh Micich
+ * @author Cédric Walter
  */
 public final class Value extends Fixed1ArgFunction {
 
@@ -65,6 +66,7 @@ public final class Value extends Fixed1ArgFunction {
                boolean foundCurrency = false;
                boolean foundUnaryPlus = false;
                boolean foundUnaryMinus = false;
+               boolean foundPercentage = false;
 
                int len = strText.length();
                int i;
@@ -123,8 +125,13 @@ public final class Value extends Fixed1ArgFunction {
                        }
                        switch (ch) {
                                case ' ':
-                                       String remainingText = strText.substring(i);
-                                       if (remainingText.trim().length() > 0) {
+                                       String remainingTextTrimmed = strText.substring(i).trim();
+                    // support for value[space]%
+                    if (remainingTextTrimmed.equals("%")) {
+                        foundPercentage= true;
+                        break;
+                    }
+                    if (remainingTextTrimmed.length() > 0) {
                                                // intervening spaces not allowed once the digits start
                                                return null;
                                        }
@@ -162,6 +169,9 @@ public final class Value extends Fixed1ArgFunction {
                                        sb.append(strText.substring(i));
                                        i = len;
                                        break;
+                case '%':
+                    foundPercentage = true;
+                    break;
                                default:
                                        // all other characters are illegal
                                        return null;
@@ -179,6 +189,7 @@ public final class Value extends Fixed1ArgFunction {
                        // still a problem parsing the number - probably out of range
                        return null;
                }
-               return new Double(foundUnaryMinus ? -d : d);
+        Double result = new Double(foundUnaryMinus ? -d : d);
+        return foundPercentage ? result /100 : result;
        }
 }
index f85db0cfff55e2383bded43ed094ced7aaa073d5..9c778d7e1321278f9d0c39aa44cdc1c7fa1248eb 100644 (file)
@@ -28,6 +28,7 @@ import org.apache.poi.ss.formula.eval.ValueEval;
  * Tests for {@link Value}
  *
  * @author Josh Micich
+ * @author Cédric Walter
  */
 public final class TestValue extends TestCase {
 
@@ -71,6 +72,8 @@ public final class TestValue extends TestCase {
                confirmValue("1,000e2", 100000);
                confirmValue("$10e2", 1000);
                confirmValue("$1,000e2", 100000);
+        confirmValue("30%", 0.3);
+        confirmValue("30 %", 0.3);
        }
 
        public void testErrors() {