Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

非浮点型科学计数解析异常处理 #195

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/main/java/com/ql/util/express/parse/ExpressParse.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,12 @@ public List<ExpressNode> transferWord2ExpressNode(ExpressPackage rootExpressPack
tempWord = tempWord.substring(0, tempWord.length() - 1);
objectValue = Long.valueOf(tempWord);
} else {
long tempLong = Long.parseLong(tempWord);
long tempLong;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个地方转成double可能更合适,基于一下两点:

  1. 2e-2,这个是0.02,转成long直接是0了
  2. 科学计数法标识的数值可以很大,超过long的最大值。

if (tempWord.indexOf("e") >=0 || tempWord.indexOf("E") >=0) {
tempLong = Double.valueOf(tempWord).longValue();
} else {
tempLong = Long.parseLong(tempWord);
}
if (tempLong <= Integer.MAX_VALUE && tempLong >= Integer.MIN_VALUE) {
tempType = nodeTypeManager.findNodeType("CONST_INTEGER");
objectValue = (int)tempLong;
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/com/ql/util/express/bugfix/ScientificNumberTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.ql.util.express.bugfix;

import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import com.ql.util.express.IExpressContext;
import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
* created by jiwenxing on 2022/5/12
*/
public class ScientificNumberTest {

static List<String> express = new ArrayList<>();
static {
express.add("2e2==200");
express.add("2E2==200");
express.add("2.0e2==200");
express.add("2.0E2==200");
}

@Test
public void testFunction() throws Exception {
ExpressRunner runner = new ExpressRunner();
IExpressContext<String, Object> context = new DefaultContext<>();
for (String exp: express) {
Object result = runner.execute(exp, context, null, false, false);
Assert.assertTrue((Boolean)result);
}
}
}