calculate

Post your questions and help other users.

Moderator: Martin

Post Reply
User avatar
Rafi4
Posts: 281
Joined: 01 Dec 2017 05:23

calculate

Post by Rafi4 » 21 Jan 2019 14:04

hi
I want to create a flow to calculate digits.
example 2+4=6
2×7=14
9-4=5
6÷6=1

how can I calculate above digits using input Dialog number, single choice menu and script? how can I create ? I am not getting success.

from record4
Last edited by Rafi4 on 23 Jan 2019 03:12, edited 1 time in total.
No.1 Automation app in play store Automagic Premium
Samsung Galaxy j2 non rooted.
Android 5.1.1

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: calculate

Post by Desmanto » 21 Jan 2019 17:28

Do you want a single input and directly get result? or you want to do some choice for the operator? (+ - x : etc?)

If you can just simply input the whole correct expression, using single line text. Just input the whole calculation at there (example : 2*7), and eval(value) afterward.

Code: Select all

x = eval(value);
Then put x at the notification on screen to show the result.

You can catch the error from the script, add another notif to tell that the input is not valid.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

User avatar
Rafi4
Posts: 281
Joined: 01 Dec 2017 05:23

Re: calculate

Post by Rafi4 » 22 Jan 2019 06:46

hi desmanto
when I calculating 7/3 . it was showing result as 2 only. how can I get result as 2.33.

thanks from record4
No.1 Automation app in play store Automagic Premium
Samsung Galaxy j2 non rooted.
Android 5.1.1

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: calculate

Post by Desmanto » 22 Jan 2019 18:01

That is a little complicated. You must know how you to input the calculation. Common division will always result in integer. You must have one of the number involved in division to have decimal. One way is to append '1.0 * ' to the value before calculation.

So the script now become

Code: Select all

x = eval('1.0 * ' + value);
The x is later eval to 1.0 * 7/3. Which will be 7.0/3 and the result is in floating number. But this will give 2.3333333333333335. You want only the the last 2 decimal. So you will show it up in 2 decimals format.

Code: Select all

x = "{eval('1.0 * ' + value),numberformat,0.00}"
The result now become 2.33

This maybe doesn't cover all situation, but should be good for most cases.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Post Reply