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.
Text input to arithmetic calculation
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Re: Text input to arithmetic calculation
Use the value() function:
answer value(fld "myMathFormula")
Andreas
answer value(fld "myMathFormula")
Andreas

Re: Text input to arithmetic calculation
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
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
-
- Posts: 5
- Joined: Sun Aug 04, 2013 4:13 pm
Re: Text input to arithmetic calculation
Thanks guys. Paul's tip was very helpful.
Mike
Mike