# Math Not working

Discussion in 'iOS Programming' started by AGWright, Aug 27, 2012.

1. Aug 27, 2012
Last edited by a moderator: Aug 27, 2012

### AGWright macrumors newbie

Joined:
Jun 28, 2010
#1
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

2. ### jnoxx macrumors 65816

Joined:
Dec 29, 2010
Location:
Aartselaar // Antwerp // Belgium
#2
Why do you use strings instead of NSNumbers/Ints/Floats?

### Staff Member

Joined:
Aug 16, 2005
Location:
New England
#3
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

4. ### AGWright thread starter macrumors newbie

Joined:
Jun 28, 2010
#4
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

### Staff Member

Joined:
Aug 16, 2005
Location:
New England
#5
Break the problem down. Does it work consistently for a simpler expression, e.g. 2+2?

B

6. ### AGWright thread starter macrumors newbie

Joined:
Jun 28, 2010
#6
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

### Staff Member

Joined:
Aug 16, 2005
Location:
New England
#7
What does it do for 9*10? How about (10*9)?

B

### Staff Member

Joined:
Sep 2, 2004
Location:
The Centennial State
9. ### chown33 macrumors 604

Joined:
Aug 9, 2009
#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.

10. ### AGWright thread starter macrumors newbie

Joined:
Jun 28, 2010
#10
found out that replacing any spaces worked (why it was putting spaces in who knows)

stringByReplacingOccurrencesOfString" " withString""];

now working thank to all who answered

### Staff Member

Joined:
Aug 16, 2005
Location:
New England
#11
So were there embedded spaces in that? Where? Can you copy and paste the actual faulty input.

B

12. ### Duncan C macrumors 6502a

Joined:
Jan 21, 2008
Location:
Northern Virginia
#12

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.:

Joined:
Jun 28, 2010