1

I want to convert a string for example

NSString *stringWithNumber = @"3/4"

into a NSNumber. How is this possible?

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
mooco
  • 51
  • 5
  • what do you want to convert it to? `3`? `4`? `0.75`? – Gabriele Petronella Jun 27 '13 at 15:19
  • What you want is an expression interpreter/evaluator. I don't know of any "standard" one on iOS, but I think there are a few open source ones floating around. – Hot Licks Jun 27 '13 at 15:25
  • 2
    For `@"3.0/4.0"` you could use `NSExpression`, as shown for example here: http://stackoverflow.com/a/12500518/1187415. But for your input that would do a integer division and produce zero. - See also [What is a fast C or Objective-C math parser?](http://stackoverflow.com/questions/4892152/what-is-a-fast-c-or-objective-c-math-parser). – Martin R Jun 27 '13 at 15:26
  • See this question: http://stackoverflow.com/questions/12064103/iphone-ios-is-there-an-expression-parser-for-an-algebraic-calculator-like-app – Hot Licks Jun 27 '13 at 15:26
  • @HotLicks correct. There are some around, but if the use case is restricted to fractions, it may be too much of an effort ;) – Gabriele Petronella Jun 27 '13 at 15:28

2 Answers2

5

You can use an NSEXpression to "calculate" the value. Note that you will have the regular int division problem with "3/4".

NSExpression *expression = [NSExpression expressionWithFormat:@"3.0/4.0"];
NSNumber *result = [expression expressionValueWithObject:nil context:nil];
David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
  • 2
    sleek, even though is the input string is `3/4`, as you already noted, it won't work as expected. – Gabriele Petronella Jun 27 '13 at 15:29
  • 1
    I have solved `int` problem in this case. See this - [Convert to Float and Calculate](http://stackoverflow.com/questions/14155360/convert-to-float-and-calculate/14155699#14155699) – TheTiger Jun 27 '13 at 15:42
4

If you are only working with n/m fractions, and you mean to have a number representing the result of the fraction, you can do something like

NSString *fraction = @"3/4";
NSArray *components = [fraction componentsSeparatedByString:@"/"];
float numerator = [components[0] floatValue];
float denominator = [components[1] floatValue];
NSNumber *result = @(numerator/denominator);
NSLog(@"%@", result); // => 0.75

Of course this can easily break in case of malformed strings, so you may want to check the format before performing the above computation.

NOTE

In case the fractions coming in input have a format compatible with native float division, David's answer is definitely sleeker and less clunky than mine. Although if you have an input like @"3/4", it won't work as expected and you definitely need to do something like I suggested above.

Bottom line, you should specify better your requirements.

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
  • Your answer will work in case of 3/4, 5/6 .... n/m only. What if string be in other formats ? – TheTiger Jun 27 '13 at 15:49
  • Quoting my own answer. *Of course this can easily break in case of malformed strings, so you may want to check the format before performing the above computation.* Without further information from the OP, there's no much more I can say. I frankly don't think my answer deserves a downvote, since it addresses the question as exposed by the OP. – Gabriele Petronella Jun 27 '13 at 15:50
  • The Question is **I want to convert a string into NSNumber for example ____** and for example means this is just an example. – TheTiger Jun 27 '13 at 15:52
  • 1
    Again, without requirements you can't really say anything more. I cannot read minds and neither can you. What makes you think that he needs more than `n/m` fractions? This is a reasonable answer to a poorly formulated question – Gabriele Petronella Jun 27 '13 at 15:54
  • Oh dude I didn't want to down vote It happen by mistake and now I cant get it back until you make an edit ... And of course answer is specific. I think answer should be for all **string to number**. Programmer would not like to make different code for every different formula string. Hope you are getting !! – TheTiger Jun 27 '13 at 15:59
  • I get it, but programmers should't shoot with a bazooka to a fly either. If you have a very specific problem, a super-generic answer may not be the best one. Again, lack of clear requirements is the biggest issue here. – Gabriele Petronella Jun 27 '13 at 16:02