Objective-C Float Rounding

前端 未结 7 1485
执念已碎
执念已碎 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:38

    Use the C standard function family round(). roundf() for float, round() for double, and roundl() for long double. You can then cast the result to the integer type of your choice.

提交回复
热议问题