java 字符串公式

zbamei2373 2011-05-09 07:24:48
本人小白 ,有个问题需要请教
*2*3/4+5% 这个字符串 如何在javascript 中或者后台 和数字进行计算
如:我知道在javascript中 可以用eval()函数
但是我这个字符串公式中带有%号,而且不知道%是在这个字符串什么地方出现,eval()只能做四则运算。好像参数中不能带有% 出现。有没有类似eval()的函数可以计算我这个字符串。不知道表达清楚没,
...全文
258 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
sxl535 2011-07-29
  • 打赏
  • 举报
回复
v5的7楼
  • 打赏
  • 举报
回复
int i=10;
String str="*2*3/4\u002b5%";
String str2=i+str;
System.out.println(str2);
zbamei2373 2011-05-10
  • 打赏
  • 举报
回复
如: 我在文本框输入一个数字 10 和"*2*3/4+5%" 这个公式做运算。10*2*3/4+5%=15.05.
"*2*3/4+5%" 是一个字符串 ,但是作用是一个公式的作用。
龙四 2011-05-10
  • 打赏
  • 举报
回复
那成什么了?你自己试试?


[Quote=引用 11 楼 yangxuan0418 的回复:]

引用 10 楼 ticmy 的回复:
这种直接repalceAll是欠考虑的,譬如:

"3/5%",本来是要3/0.05,结果repalceAll就变成3/5*0.01了




引用 8 楼 shidanwo77 的回复:

引用 5 楼 zbamei2373 的回复:
有没有什么函数 可以把“*2*3/4+5%” 变成 *2*3/4+0.05
例二:
*2*40%……
[/Quote]
yangxuan18 2011-05-10
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 ticmy 的回复:]
这种直接repalceAll是欠考虑的,譬如:

"3/5%",本来是要3/0.05,结果repalceAll就变成3/5*0.01了




引用 8 楼 shidanwo77 的回复:

引用 5 楼 zbamei2373 的回复:
有没有什么函数 可以把“*2*3/4+5%” 变成 *2*3/4+0.05
例二:
*2*40%*34/3 变成 *2*0.4*34/3
……
[/Quote]

改为 str=str.replace("%","(1*0.01)");
龙四 2011-05-10
  • 打赏
  • 举报
回复
这种直接repalceAll是欠考虑的,譬如:

"3/5%",本来是要3/0.05,结果repalceAll就变成3/5*0.01了



[Quote=引用 8 楼 shidanwo77 的回复:]

引用 5 楼 zbamei2373 的回复:
有没有什么函数 可以把“*2*3/4+5%” 变成 *2*3/4+0.05
例二:
*2*40%*34/3 变成 *2*0.4*34/3


String str="*2*3/4+5%";
str=str.replace("%","*0.01");
System.out.println(str);
[/Quote]
  • 打赏
  • 举报
回复
public static void main(String[] args) throws ScriptException {
int i=10;
String str="*2*3/4\u002b5%";
String str2=i+str;
str2=str2.replace("%","*0.01");
System.out.println(str2);
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
Object result = engine.eval(str2);
System.out.println("结果类型:" + result.getClass().getName() + ",计算结果:" + result);
}
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zbamei2373 的回复:]
有没有什么函数 可以把“*2*3/4+5%” 变成 *2*3/4+0.05
例二:
*2*40%*34/3 变成 *2*0.4*34/3
[/Quote]

String str="*2*3/4+5%";
str=str.replace("%","*0.01");
System.out.println(str);
龙四 2011-05-10
  • 打赏
  • 举报
回复
若是jdk1.5版本,可以这样做:

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class Calculator {
private static String parseExpress(String express) throws IllegalArgumentException {
if(express == null || express.trim().length() == 0) {
throw new IllegalArgumentException("参数非法,express:" + express);
}
Pattern pattern = Pattern.compile("(\\d+)\\%");
Matcher matcher = pattern.matcher(express);
while(matcher.find()) {
express = matcher.replaceAll("(" + matcher.group(1) + "*0.01)");
}
return express;
}


public static Object calculate(String exp) throws Exception {
exp = parseExpress(exp);
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("jexl");
return engine.eval(exp);
}


public static void main(String... a) throws Exception {
Object result = calculate("10*2*40%*34/3");
System.out.println("结果:" + result + " ; 类型:" + result.getClass().getName());
}
}





下载几个文件:commons-logging-1.1.1.jar、commons-jexl-2.0.1.jar、bsf-api-3.1.jar

若在控制台上运行可以这样:java -cp .;commons-logging-1.1.1.jar;commons-jexl-2.0.1.jar;bsf-api-3.1.jar Calculator


若是在eclipse中,将这几个文件加入classpath即可
龙四 2011-05-10
  • 打赏
  • 举报
回复
后台可以这样做:

需jdk1.6版本
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class Calculator {
private static String parseExpress(String express) throws IllegalArgumentException {
if(express == null || express.trim().length() == 0) {
throw new IllegalArgumentException("参数非法,express:" + express);
}
Pattern pattern = Pattern.compile("(\\d+)\\%");
Matcher matcher = pattern.matcher(express);
while(matcher.find()) {
express = matcher.replaceAll("(" + matcher.group(1) + "*0.01)");
}
return express;
}


public static Object calculate(String exp) throws Exception {
exp = parseExpress(exp);
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
return engine.eval(exp);
}


public static void main(String... a) throws Exception {
Object result = calculate("10*2*40%*34/3");
System.out.println("结果:" + result + " ; 类型:" + result.getClass().getName());
}
}

zbamei2373 2011-05-10
  • 打赏
  • 举报
回复
有没有什么函数 可以把“*2*3/4+5%” 变成 *2*3/4+0.05
例二:
*2*40%*34/3 变成 *2*0.4*34/3
龙四 2011-05-09
  • 打赏
  • 举报
回复
首先,你给我算一下“*2*3/4+5%”的结果
devin_jia 2011-05-09
  • 打赏
  • 举报
回复
你要怎么计算是把“*2*3/4+5%”做为字符串?

67,541

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧