I want to calculate a string, which I'm doing by this:
NSExpression *expression = [NSExpression expressionWithFormat:calculationString];
float result = [[expression expressionValueWithObject:nil context:nil] floatValue];
NSLog(@"%f", result);
The problem is, when calculationstring is 1/2, the result is 0. I tried to change float with double and NSNumber and the %f to %f and %@, but I always just get 0. What to I have to change?
Also if it matters, I am in Europe, so I have commas instead of points for this value, but it shouldn't matter as I am logging with %f which shows it as points. Just for information
I think You need to User DDMathParser Which is best in this situation. I have used it in One of my project which is facing same problem as you have faced
DDMathEvaluator *eval = [DDMathEvaluator defaultMathEvaluator]; id value=[eval evaluateString:@"1/2" withSubstitutions:nil error:&error]; NSLog(@"Result %@",value); Result 0.5
Typing in
NSExpression
is much like in C: literals that look like integers (no decimal point/comma) are treated as integers and thus use integer division. (Under integer division, 1/2 is zero. If you want 0.5, you need floating point division.) This happens when the expression is parsed and evaluated, so attempting to change the type of the result or the formatting of the output has no effect -- those things happen after parsing and evaluation.If your
calculationString
is entirely under your control, it's easy to make sure that you use floating point literals anywhere you want floating point division. (That is, use1.0/2
instead of1/2
.) If not, you'll need to change it such that it does -- here it's probably better to decompose the parsedNSExpression
and change an operand rather than munge the string.Followup edit on the "decompose" bit: String munging in content that you know to have higher-order structure is generally problematic. And with
NSExpression
, you already have a parser (who's smarter than a simple regex) decomposing the string for you — that is in fact whatNSExpression
is all about.So, if you're working with a user-provided string, don't try to change the expression by changing the string. Let
NSExpression
parse it, then use properties of the resulting object to pick it apart into its constituent expressions. If your string is simply "1/2", then your expression has an array of twoarguments
and the function"divide:by:"
— you can replace it with an equivalent function where one of the arguments is explicitly a floating-point value:Basically, you just need to tell it that you are performing floating point operation,
Will all work