Doing math in vb.net like Eval in javascript -


is there way parse string in vb.net (like, built in methods), can math eval can? example, 3+(7/3.5) string return 2.

i not asking code me, want know if there built in way this, if there not code myself.

i can wager not able parse stuff sin(90) on own, , understand need replaced math.sin(90).

if there built in method, how use it?

there's shortcut limited (ie. simple) math expressions using datatable.compute method. obviously, isn't robust (limited functionality) , feels hackish misuse datatable purpose, figured add current answers.

example:

var result = new datatable().compute("3+(7/3.5)", null); // 5 

"sin(90)" wouldn't work approach. refer datacolumn.expression property page list of supported functions, under "aggregates" section.

using system.codedom namespace option.

some helpful links:


edit: address comment, here approach demonstrate replacing trigonometric functions equivalent math class methods.

c#

string expression = "(sin(0) + cos(0)+tan(0)) * 10"; string updatedexpression = regex.replace(expression, @"(?<func>sin|cos|tan)\((?<arg>.*?)\)", match =>             match.groups["func"].value == "sin" ? math.sin(int32.parse(match.groups["arg"].value)).tostring() :             match.groups["func"].value == "cos" ? math.cos(int32.parse(match.groups["arg"].value)).tostring() :             math.tan(int32.parse(match.groups["arg"].value)).tostring()         ); var result = new datatable().compute(updatedexpression, null); // 10 

vb.net

dim expression string = "(sin(0) + cos(0)+tan(0)) * 10" dim updatedexpression string = regex.replace(expression, "(?<func>sin|cos|tan)\((?<arg>.*?)\)", function(match match) _         if(match.groups("func").value = "sin", math.sin(int32.parse(match.groups("arg").value)).tostring(), _         if(match.groups("func").value = "cos", math.cos(int32.parse(match.groups("arg").value)).tostring(), _         math.tan(int32.parse(match.groups("arg").value)).tostring())) _         ) dim result = new datatable().compute(updatedexpression, nothing) 

note, however, need know contents of "arg" group. know ints, used int32.parse on them. if combination of items simple approach won't work. suspect need band-aid solution if gets complicated more unsupported function calls, in case codedom approach or others may more suitable.


Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -