I have already implemented a custom calculator where I am using following code to evaluate the arithmetic expression something like 5+3*5-3.
- (NSNumber *)evaluateArithmeticStringExpression:(NSString *)expression {
NSNumber *calculatedResult = nil;
@try {
NSPredicate * parsed = [NSPredicate predicateWithFormat:[expression stringByAppendingString:@" = 0"]];
NSExpression * left = [(NSComparisonPredicate *)parsed leftExpression];
calculatedResult = [left expressionValueWithObject:nil context:nil];
}
@catch (NSException *exception) {
NSLog(@"Input is not an expression...!");
}
@finally {
return calculatedResult;
}
}
But when I am having integers with division operation I am getting only integer as a result. Let's say for 5/2 I am getting 2 as a result. It's right for the shake of programming because of the integer division.
But I need floating point result.
How can I get it rather scanning the expression string and replace the integer divisor as a floating point. In our example 5/2.0 or 5.0/2.