I am using ostringstream to output a number to 2 decimal places as follows

std::ostringstream ostr;

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

So, if cylinderLength = 0.594, the above outputs 0.59 as expected.

Is there an operator or function that will round up or down in the last desired decimal place?

In this case, the above example would print out 0.60 instead.

有帮助吗?

解决方案

Try:

// 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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top