iOS技术

iOS开发常用算法

2018-07-16  本文已影响43人  Leeson1989

1、四舍五入法 roundf

float number;

int result;

 number =10.51;

 result = (int)roundf(number);

NSLog(@"roundf(%.2f) = %d", number, result);//输出 roundf(10.51) = 11

number =10.41;

 result = (int)roundf(number);

NSLog(@"roundf(%.2f) = %d", number, result);//输出 roundf(10.41) = 10


2、进位算法  ceilf  将小数整数位+1

float number;

int result;

 number =9.61;

 result = (int)ceilf(number);

NSLog(@"ceilf(%.2f) = %d", number, result);//输出 ceilf(9.61) = 10

number =9.41; 

result = (int)ceilf(number);

NSLog(@"ceilf(%.2f) = %d", number, result);//输出 ceilf(9.41) = 10


3、摸位方法 floorf 常说的取整

float number;

int result; 

 number =10.61; 

 result = (int)floorf(number);

NSLog(@"floorf(%.2f) = %d", number, result);//输出 floorf(10.61) = 10

number =10.41; 

result = (int)floorf(number);

NSLog(@"floorf(%.2f) = %d", numberToRound, result);//输出 floorf(10.41) = 10

上一篇下一篇

猜你喜欢

热点阅读