Frage

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.

War es hilfreich?

Lösung

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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top