在Objective-C中将float转换为int

Str*_*boy 10 objective-c

如何将a转换floatint舍入到下一个整数?例如,1.00001将转到2,而1.9999将转到2.

iOS*_*dev 63

float myFloat = 3.333

// for nearest integer rounded up (3.333 -> 4):
int result = (int)ceilf(myFloat );

// for nearest integer (3.4999 -> 3, 3.5 -> 4):
int result = (int)roundf(myFloat );

// for nearest integer rounded down (3.999 -> 3):
int result = (int)floor(myFloat);

// For just an integer value (for which you don't care about accuracy) 
int result = (int)myFloat;
Run Code Online (Sandbox Code Playgroud)


Vla*_*mir 11

使用ceil函数:

int intValue = (int)ceil(yourValue);
Run Code Online (Sandbox Code Playgroud)