round()函数的实现

版权声明:原创博文,转载请注明出处! https://blog.csdn.net/sunriver2000/article/details/80377998
2.1 2.6 -2.1 -2.6
floor() 不大于自变量的最大整数 2 2 -3 -3
ceil() 不小于自变量的最大整数 3 3 -2 -2
round() 四舍五入到最邻近的整数 2 3 -2 -3

floor(),ceil() 需包含头文件math.h

C++中如果没有round函数,则需自己建立。

double round(double r)
{
    return (r > 0.0) ? floor(r + 0.5) : ceil(r - 0.5);
}

猜你喜欢

转载自blog.csdn.net/sunriver2000/article/details/80377998