加与变量同符号的0.5,再强制取整就可以了。如下面的举例代码:
//#include "stdafx.h"//If the vc++6.0, with this line.
#include "
stdio.h"
int main(void){
double x=3.1415926,y=1.507,z=0.499999;
printf("%d %d %d\n",int(x+(x>0?.5:-.5)),int(y+(y>0?.5:-.5)),int(z+(z>0?.5:-.5)));
return 0;
}