0

I have the following code:

NSExpression *expression;

@try {
    expression = [NSExpression expressionWithFormat:@"20/100*200"];

    NSNumber *result = [expression expressionValueWithObject:nil context:nil];
}
@catch(NSException *exception){}

Somehow, the result I'm getting back is an NSNumber of 0, instead of 40. What am I doing wrong?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
mattsven
  • 22,305
  • 11
  • 68
  • 104

1 Answers1

2

It's doing an integer division instead of a floating point one. Try this:

expression = [NSExpression expressionWithFormat:@"20.0/100*200"];
Code Different
  • 90,614
  • 16
  • 144
  • 163