I did not run this through the compiler to double check it, but here is the main point of the algorithm (transformed from the answer to this question ):
-(float) round:(float)num toSignificantFigures:(int)n { if(num == 0) { return 0; } double d = ceil(log10(num < 0 ? -num: num)); int power = n - (int) d; double magnitude = pow(10, power); long shifted = round(num*magnitude); return shifted/magnitude; }
It is important to remember that Objective-C is a superset of C, so everything that is valid in C is also valid in Objective-C. This method uses the C functions defined in math.h
source share