Reputation: 2058
I am using the NSExpression
function to evaluate strings as a numerical equation.
One problem that I have encountered is; when you have a division "/" operation in the string you are calculating NSExpression
does not convert the answer into a float point (double) unless a decimal value is given.
Ex. ( 1.0 / 2 ) = 0.5
But ( 1 / 2 ) = 0
I wonder if there is a simple solution to this problem or if I would have to write a loop that inserts ".0" when it detects a division with no other decimals.
I have tried making a loop like this with little success, so if there is a better solution or someone has a loop structure to solve this problem, it would be greatly appreciated.
Thanks, Regards.
Upvotes: 0
Views: 476
Reputation: 62676
How about, if you detect the presence of slash, cast your expression like this:
myExpressionStr = [[@"(double)(" stringByAppendingString:myExpressionStr] stringByAppendingString:@")"];
Upvotes: 0
Reputation: 10124
Have you considered DDMathParser? From what I found it does a lot of work for you...
Upvotes: 2