Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mongrol

macrumors regular
Original poster
Jul 16, 2007
232
0
Can anyone tell me the most efficient way to make a negative NSNumber positive?

If I have -1.5 I want to cheaply make it 1.5. Also If I feed 1.5 into the same function I still want 1.5

Cheating by subtracting the same number twice is not what I'm looking for. :)
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
NSNumber is a wrapper. You cannot perform arithmetic operations on it.
Code:
+(NSNumber *)absoluteValue:(NSNumber *)input {
  return [NSNumber numberWithDouble:fabs([input doubleValue])];
}

I don't know the "type" you initialized your input with, so you may want to adjust to numberWithFloat, etc. instead.

You will need to include/import math.h for fabs or fabsf.

-Lee
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.