How to round off Float values? [duplicate]
Possible Duplicate:
Rounding numbers in Objective-C
In Objective-c How to round off Float values?
In addition to the other answers:
float theFloat = 1.23456;
int rounded = roundf(theFloat); NSLog(@"%d",rounded);
int roundedUp = ceil(theFloat); NSLog(@"%d",roundedUp);
int roundedDown = floor(theFloat); NSLog(@"%d",roundedDown);
// Note: int can be replaced by float
For rounding to specific decimals, see the question mentioned by Alex Kazaev.
The function lroundf()
will do it:
float a=20.49;
int myInt = lroundf(a);
Convert to int and then convert back to float.
CGFloat *myFloat = 100.765;
NSInteger *myInteger = myFloat;
CGFloat *newFloat = myInteger;
This will work