Objective-C Float Rounding

前端 未结 7 1510
执念已碎
执念已碎 2020-12-16 09:26

How might I round a float to the nearest integer in Objective-C:

Example:

float f = 45.698f;
int rounded = _______;
NSLog(@\"the rounded float is %i\         


        
7条回答
  •  悲哀的现实
    2020-12-16 09:42

    The easiest way to round a float in objective-c is lroundf:

    float yourFloat = 3.14;
    int roundedFloat = lroundf(yourFloat); 
    NSLog(@"%d",roundedFloat);
    

提交回复
热议问题