Page 1 of 1

Text input to arithmetic calculation

Posted: Sun Aug 04, 2013 4:18 pm
by thekillerapp6046
Hi livecoders,

If i input to a text field say, "1+2-3/4*5". Can I perform the arithmetics directly and display the answer without manually "processing" the data as a string? Thank you.

Mike.

Re: Text input to arithmetic calculation

Posted: Sun Aug 04, 2013 4:41 pm
by append[x]
Use the value() function:

answer value(fld "myMathFormula")

Andreas

Re: Text input to arithmetic calculation

Posted: Sun Aug 04, 2013 10:40 pm
by paul_gr
Allowing the user to enter a mathematical expression from the keyboard can be quite difficult, as you will have to trap accidental input errors.
These input errors will either give an erroneous output or crash the app.

Value(expression) looks like a simple solution, but will not work unless the input is guaranteed to have no mathematic errors.

for example, double operators will not give a true result
value(12++4) will give an execution error.
value(12//3) will give an output of 12.

I would catch input errors using a try catch block; here's a simple example...

on mouseUp
try
answer value(fld "myMathFormula")
catch errorcatch
put " math error in input string"
end try
end mouseUp

I would also limit user input to numbers "0-9", decimal points and the operators "-+/*" if no trig functions like sin,cos,tan etc are required.

cheers
Paul

Re: Text input to arithmetic calculation

Posted: Tue Aug 06, 2013 8:40 pm
by thekillerapp6046
Thanks guys. Paul's tip was very helpful.

Mike