Objective-c: How to round Float values?

Possible duplicate:
Number of rounding in Objective-C

In Objective-c How to round Float values?

+42
objective-c
Apr 08 2018-11-11T00:
source share
3 answers

In addition to 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 decimal places, see the question mentioned by Alex Kazaev.

+139
Apr 08 2018-11-11T00:
source share
— -

The lroundf() function will do this:

 float a=20.49; int myInt = lroundf(a); 
+18
Apr 08 '11 at 11:01
source share

Convert to int and then convert back to float.

 CGFloat *myFloat = 100.765; NSInteger *myInteger = myFloat; CGFloat *newFloat = myInteger; 

It will work

+1
Apr 08 2018-11-11T00:
source share



All Articles