# iOSMath Not working

#### AGWright

##### macrumors newbie
Original poster
Hi

if I use this (Hardcoding MySum)
Code:
`````` NSString *mySum = @"(10+10)+25*7";
NSNumber *result = [mySum numberByEvaluatingString];
the text box correctly displays 195

while if I build up a string to be the same @"(10+10)+25*7"
I get the answer of 70
Code:
``````    NSString *mySum = [NSString stringWithFormat:@"%@",MyAnswer];
NSNumber *result = [mySum numberByEvaluatingString];

any Ideas

Best Regards

Last edited by a moderator:

#### jnoxx

##### macrumors 65816
Why do you use strings instead of NSNumbers/Ints/Floats?

#### balamw

##### Moderator
Staff member
Why do you use strings instead of NSNumbers/Ints/Floats?
A bit of context would definitely be helpful, but I suspect the OP is trying to make an app where the user can enter an arbitrary formula and have it evaluated.

B

#### AGWright

##### macrumors newbie
Original poster
A bit of context would definitely be helpful, but I suspect the OP is trying to make an app where the user can enter an arbitrary formula and have it evaluated.

B
Yep sorry the user get a series of numbers and have to make it add up to a target
I.e target = 195
Sum = (10+10) + 25 * 7

#### balamw

##### Moderator
Staff member
Break the problem down. Does it work consistently for a simpler expression, e.g. 2+2?

B

#### AGWright

##### macrumors newbie
Original poster
Break the problem down. Does it work consistently for a simpler expression, e.g. 2+2?

B
yep works if I do 2+2 and 9*9 but if I do 10*9 I get 0 so tried 12*9 and I got 18
so its not 'seeing' the 1 as in 12 but just sees 2*9

#### balamw

##### Moderator
Staff member
yep works if I do 2+2 and 9*9 but if I do 10*9 I get 0 so tried 12*9 and I got 18
so its not 'seeing' the 1 as in 12 but just sees 2*9
What does it do for 9*10? How about (10*9)?

B

Staff member

#### chown33

##### Moderator
Staff member
yep works if I do 2+2 and 9*9 but if I do 10*9 I get 0 so tried 12*9 and I got 18
so its not 'seeing' the 1 as in 12 but just sees 2*9
Break it down more. Run more tests, like 9*12 or 32*9 or 200*9, or put a space before 12*9.

And when you do these tests, post the code for them, as well as the results. Your first post failed to show the code that builds the string. Seeing the string-builder code might be a valuable clue. In addition, someone else could actually compile and test the code themselves, rather than you being the only one who can do it.

Also please tell us where you got the code that adds the numberByEvaluatingString capability. AFAICT it's not a standard NSString method. Sometimes people get older versions of third-party libraries, and see bugs that have been fixed in newer versions.

#### AGWright

##### macrumors newbie
Original poster
yep works if I do 2+2 and 9*9 but if I do 10*9 I get 0 so tried 12*9 and I got 18
so its not 'seeing' the 1 as in 12 but just sees 2*9
found out that replacing any spaces worked (why it was putting spaces in who knows)

stringByReplacingOccurrencesOfString" " withString""];

now working thank to all who answered

#### balamw

##### Moderator
Staff member
So were there embedded spaces in that? Where? Can you copy and paste the actual faulty input.

B

#### Duncan C

##### macrumors 6502a
Hi

if I use this (Hardcoding MySum)
Code:
`````` NSString *mySum = @"(10+10)+25*7";
NSNumber *result = [mySum numberByEvaluatingString];
the text box correctly displays 195

while if I build up a string to be the same @"(10+10)+25*7"
I get the answer of 70
Code:
``````    NSString *mySum = [NSString stringWithFormat:@"%@",MyAnswer];
NSNumber *result = [mySum numberByEvaluatingString];

any Ideas

Best Regards

Where does teh numberByEvaluatingString method come from? I don't see it in any of Apple's frameworks. Searching the net, I found something called DDMathParser. Is that it?

For future reference, its a REALLY good idea to list any third party libraries that you are using, along with a link to info on those libraries. Otherwise, we have no idea what you are using.

You should also provide a brief summary of those libraries, what functions they provide, and how you think they are supposed to work.

e.g.:

The function numberByEvaluatingString comes from a library called DDMathParser that I found on github <link>. It lets me take formulas as strings and evaluate them as numeric expressions.