iOS round函数 需要导入什么框架

2025-04-25 03:47:43
推荐回答(1个)
回答1:

iOS round函数是在默认的math.h文件中的。
Objective-C做为ANSI C的扩展,使用C标准库头文件中定义的数学常量宏及数学函数来实现基本的数学计算操作,所以不必再在Cocoa Foundation中寻找相应的函数和类了。
常用函数:
//指数运算
NSLog(@"%.f", pow(3,2) ); //result 9
NSLog(@"%.f", pow(3,3) ); //result 27

//开平方运算
NSLog(@"%.f", sqrt(16) ); //result 4
NSLog(@"%.f", sqrt(81) ); //result 9

//上舍入
NSLog(@"res: %.f", ceil(3.000000000001)); //result 4
NSLog(@"res: %.f", ceil(3.00)); //result 3

//下舍入
NSLog(@"res: %.f", floor(3.000000000001)); //result 3
NSLog(@"res: %.f", floor(3.9999999)); //result 3

//四舍五入
NSLog(@"res: %.f", round(3.5)); //result 4
NSLog(@"res: %.f", round(3.46)); //result 3
NSLog(@"res: %.f", round(-3.5)); //NB: this one returns -4

//最小值
NSLog(@"res: %.f", fmin(5,10)); //result 5

//最大值
NSLog(@"res: %.f", fmax(5,10)); //result 10

//绝对值
NSLog(@"res: %.f", fabs(10)); //result 10
NSLog(@"res: %.f", fabs(-10)); //result 10

这里没有列出的三角函数也是属于C标准数学函数的一部分,也可以在中查阅。