-4

For example, I want 2.22 to round to 2 and 7.8 to round up to 8, and I want to use these values as integers later in my code to print out a certain number of asterisks. Turning them into int values rounds them down automatically, but I need a number to round up, how would I go about doing that?

1 Answers1

1

You can use a round() function, such as the one below, which works for positive numbers.

double round(double d)
{
  return floor(d + 0.5);
}

You need the floor() function for this, found in <cmath>. I honestly cannot think of anything involving JUST <iostream> and <iomanip>

EDIT: For another approach, use std::round(), from <cmath>

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88