我需要一个简单的浮点四舍五入函数,因此:
double round(double);
round(0.1) = 0
round(-0.1) = 0
round(-0.9) = -1我可以在math.h中找到ceil()和floor() --但round()找不到。
它是以另一个名称出现在标准C++库中,还是丢失了??
发布于 2010-02-12 11:02:24
您可以使用以下命令将精度舍入到n位:
double round( double x )
{
const double sd = 1000; //for accuracy to 3 decimal places
return int(x*sd + (x<0? -0.5 : 0.5))/sd;
}https://stackoverflow.com/questions/485525
复制相似问题