java计算字符串

public static Double calcStr(String expr){
    try {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("js");
        Object result = engine.eval(expr);
        if("NaN".equals(result.toString()))
            return 0.00;
        return Double.parseDouble(result+"");
    }catch (Exception e){
        return 0.00;
    }
}

你可能感兴趣的:(java计算字符串)