Is it possible to create a code where it behaves like a scientific calculator?

Greetings, is it possible to create a code where we input a string of a formula such as "(5-1)*5 and UiPath solves the formula which also follows the MDAS rule of mathematics

eg1.
Input: (5-1)*5
Output: 20

eg2.
Input: ((10-5)*(2+2))*5
Output: 100

Regards,
Wil

Hi,

Unfortunately, Vb or C# doesn’t have a function like eval(), we need to approach by other ways.

● DataTable.Compute method

strExpression = "((10-5)*(2+2))*5"
dt = New DataTable()

Then

dt.Compute(strExpression, "").ToString

● Inject JS script activity

strExpression = "((10-5)*(2+2))*5"

Inject JS Script activity

Indicate an element in any browser.

Input Parameter : strExpression
ScriptCode

 "function(e,v) {
return eval(v);
}"

ScriptOutput : strResult

●Use 3rd party library.
Perhaps we can find .net library for it in nuget feed.

Hope this helps you.

Regards,

2 Likes

Can I please have a .xaml file sample for this? It would really be helpful for me to see more

Hi,

Here you are.

Sequence1.zip (1.7 KB)

Please re-take a target element in Inject JS Script activity.

Regards,

Thank you, I tested it out and it works, I got a question though, is it necessary to always have a dt for the compute?

Hi,

Yes. As Compute method is not static method, datatable instance is necessary.

Regards,

Thank you so much, will check into this.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.