是否有标准的格式化函数或运算符进行舍入?(?)

Is there a standard formatting function or operator that will round up? (or down?)

本文关键字:舍入 运算符 标准 格式化 函数 是否      更新时间:2023-10-16

我使用ostringstream输出一个数字到小数点后2位,如下所示

std::ostringstream ostr;
ostr << std::fixed << std::setprecision(2);
ostr << cylinderLength;

因此,如果气缸长度= 0.594,则如上所述输出0.59。

是否有一个运算符或函数可以在最后一位小数点后四舍五入?

在这种情况下,上面的示例将输出0.60。

尝试:

// Round towards +infinity (to 1 decimal place (use 100 for 2 decimal places)
ceil(double * 10) / 10

// Round towards -infinity (to 1 decimal place (use 100 for 2 decimal places)
floor(double * 10) / 10