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
Typing in
NSExpressionis 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
calculationStringis 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/2instead of1/2.) If not, you’ll need to change it such that it does — here it’s probably better to decompose the parsedNSExpressionand 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 whatNSExpressionis all about.So, if you’re working with a user-provided string, don’t try to change the expression by changing the string. Let
NSExpressionparse 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 twoargumentsand the function"divide:by:"— you can replace it with an equivalent function where one of the arguments is explicitly a floating-point value: