Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us to spark ideas for how to put the latest capabilities into action. Register here!
cancel
Showing results for 
Search instead for 
Did you mean: 
GuillaumeRUE
Contributor III
Contributor III

Parse a mathematical formula

Hello,

I would like to know if it's possible to parse a string of a mathematical formula and get the result?
For example, I get from my DB something like '98+2' as a string, and I would like to print the result.

Then if it's possible, can I change a variable in the formula?
For example, if I have '(value+5)*8' , can I change value by something else, a real variable?

Thank's in advance

Guillaume

6 Replies
marcus_sommer

Try it with something like: evaluate(string) as Result

- Marcus

GuillaumeRUE
Contributor III
Contributor III
Author

This perfect for simple expressions like '50+2'.

Do you know if I can now manage to parse expressions like 'value+2'?
My goal is to change value (which is a string) by a variable.

marcus_sommer

Maybe you could a parametrized variable for it, something like:

set exEvaluate = "evaluate($1 + 2)";  /* just $1 + 2 might be also working, it's depending on your value */

and then a call like:

$(exEvaluate(value))

- Marcus

GuillaumeRUE
Contributor III
Contributor III
Author

I can try this solution.

But the problem that I see is my expression is not always 'value+2',

it can be somethng like  '(value/8)*0.5'

or '0.5*((value+4)/0.2)+89'

So I imagine the best solution should be to find the part of my string where there is the word 'value' and change it by my variable, if it's possible.

marcus_sommer

With the above mentioned approach you could use as many parameters like you want and you could also extract/strip some content from a parameter - nearly everything will be possible if you could apply a stable logic, for example:

set exEvaluate = "evaluate($1 $2 $3 $4 $5 $6 $7 $8 $9)";

$(exEvaluate(0.5, *, value, +, 4, /, 0.2, +, 89))

and I think there are also ways to define a certain calculation-order probably the easiest approach is just using severals of such customized-functions but thinkable are also other ways which contain the brackets.

- Marcus

GuillaumeRUE
Contributor III
Contributor III
Author

Ok Thank you, I will try this solution.

I'll keep you informed!