B4J Question Evaluating a math expression given in string, HELP??

ilan

Expert
Licensed User
Longtime User
hi

i would like to make a math calculation from a given string like:

B4X:
dim mathproblem as string = "(25*2)-4"
log(mathproblem)

and i should get = 46

i found a great VB source that is really working very well but i dont know how to convert "redim" and "redim preserve" to b4x language

i will attach the b4j project (vb code is inside)

can someone make a working b4x code from it??

thanx
 

Attachments

  • mathString.zip
    3.3 KB · Views: 249

ilan

Expert
Licensed User
Longtime User
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
You could also use my Nashorn library
define a js function like
B4X:
function evaluate(a,b){
    return new java.lang[b](eval(a))
}
This also allows you to specify the data type for the return
so you call it by
B4X:
   script.invoke("evaluate",Array("Math.sqrt(12*(14-6)/7)","Float"))
And in this case it will return a float value.
(You can use Double,Float,Integer,String etc)
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
You could also use my Nashorn library
define a js function like
B4X:
function evaluate(a,b){
    return new java.lang[b](eval(a))
}
This also allows you to specify the data type for the return
so you call it by
B4X:
   script.invoke("evaluate",Array("Math.sqrt(12*(14-6)/7)","Float"))
And in this case it will return a float value.
(You can use Double,Float,Integer,String etc)

thanks @Daestrum, but the class of erel is exactly what i needed because i am developing a b4i tool and his class runs on all b4x products.
i use his class also with b4j where i build some assisting tools for my new app.
 
Upvote 0
Top