iOS 小数 变成 整数

几种取整方法验证程序,如下,可以编译运行体会下
int强制转换是不保留小数的。
ceil是向上取整,floor是向上取整,这两个函数返回值是double类型的
c语言中没有四舍五入函数,需要使用时可以自己定义下。
#include <math.h>#include <stdio.h>// 自定义四舍五入宏#define ROUND(x) (int)(x + 0.4999999999999999)double round(double x, int d) // d为保留数字位数{ double y = pow(10, d); return floor(x * y + 0.5) / y;}double round(double x, double d) // d为圆整精度{ return floor(x / d + 0.5) * d;}void main(){ double a = 3.0; int i; for(i=0; i<10; i++, a+=0.1) printf("%f\t%d\t%d\t%d\t%d\n", a, (int)a, (int)ceil(a), (int)floor(a), ROUND(a)); double x = 53.1415926; for(i=-1; i<5; i++) printf("%f\t", round(x, i)); printf("%f\t", round(x, 0.037));}

posted on 2016-12-02 10:19  举个例子yi聪聪  阅读(316)  评论(0编辑  收藏  举报

导航