有没有四舍五入的函数,例   1.499999返回 2,   1.43999999返回1,-1.4999999返回-2等,急

解决方案 »

  1.   

    在VC下运行通过:const double EPSINON = 0.00001;int approximate(double x)
    {
    double y,z;
    int nRet;

    if ((x >= - EPSINON) && (x <= EPSINON))
    {
    return 0;
    }
    if(x > EPSINON)
    {
    y = x + 0.5;
    nRet = (int)y;
    z = (double )nRet + 1.0;
    z -= y; 
    if(  (z >= - EPSINON) && (z <= EPSINON) )
    {
    return nRet + 1;
    }
    else
    {
    return nRet;
    }
    }
    if(x < -EPSINON )
    {
    y = x - 0.5;
    nRet = (int)y;
    z = (double )nRet - 1.0;
    z -= y; 
    if(  (z >= - EPSINON) && (z <= EPSINON) )
    {
    return nRet -1;
    }
    else
    {
    return nRet;
    }
    }

    }
      

  2.   

    float x = 0.2165451
    int y = int(x+0.5)不过这样做的话
    -1.4999999是返回-1的
    恩??按照四舍五入,这个不应该就是返回-1吗,你要-2??
      

  3.   

    没有四舍五入的
    自己编。由于浮点数的精度问题,最好转换到整数再处理(int(f*10,*100...)).
      

  4.   

    用下面的函数:
    #include <math.h>
    double getintx(double x)
    {
    if (x>=0)
    return ceil(x);
    else
    return floor(x);
    }
    cs2005上的