This will get the correct result in any case:
NSNumber *n = @42.42;
CGFloat cgf = [n doubleValue];
because CGFloat is either float or double.
NSNumber does not have a CGFloatValue method. You could define one
using the toll-free bridge to CFNumberRef:
@interface NSNumber (MyCGFloatValue)
-(CGFloat)myCGFloatValue;
@end
@implementation NSNumber (MyCGFloatValue)
-(CGFloat)myCGFloatValue{
CGFloat result;
CFNumberGetValue((__bridge CFNumberRef)(self), kCFNumberCGFloatType, &result);
return result;
}
@end
or using the C11 feature "Generic selection", where the compiler chooses the appropriate
code depending on the type of CGFloat:
@implementation NSNumber (MyCGFloatValue)
-(CGFloat)myCGFloatValue{
CGFloat result;
result = _Generic(result,
double: [self doubleValue],
float: [self floatValue]);
return result;
}
@end
And then
NSNumber *n = @42.24;
CGFloat f = [n myCGFloatValue];
but I doubt that it is worth the hassle.