0

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.

1 Answers1

6

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
Martin York
  • 257,169
  • 86
  • 333
  • 562
  • Yes, this clearly works - thank you. But there is no function or method that does this directly? I want to experiment with different precisions of rounding so this is a bit ungainly. –  Jan 12 '13 at 06:47
  • @AndrewS. You can write a wrapper which does this. – Karthik T Jan 12 '13 at 06:50