How do I round up?

Hey, I recently started taking a c++ course and I am having problem rounding up what I am trying to do is round up to ten decade (ten's (sorry for the English).

Example if I have 50/6 and I want it to round up to 10 how do I do that?

What I want is that I can type anything in division and get it to ten decades.


Edit: typo


Thanks
Last edited on
int nine = round(50/6);

I think its in <cmath>
Last edited on
Thanks for the help, sorry but I wrote wrong. Example if I have 55/10 and I want it to round up to 6, how do I do that?

What I want is that I can type anything in division and get it to round up so if I type in my program 91/10 I want to get 10


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <cmath>

double nearest_ten(double n)
{
    return std::round(n/10.0) * 10.0;
}

using namespace std;

int main()
{
    cout << nearest_ten(50.0/6.0) << '\n';
    cout << nearest_ten(-50.0/6.0) << '\n';
}


Also, instead of round(), you might consider floor() and ceil().

http://www.cplusplus.com/reference/cmath/ceil/
http://www.cplusplus.com/reference/cmath/floor/
http://www.cplusplus.com/reference/cmath/round/

Last edited on
Article: Rounding Algorithms
http://www.cplusplus.com/forum/articles/3638/
try ceil(91/10);
that gives 10.
round gives 9.

I forget what happens with negatives, but it may not be what you want... you may need floor for negatives depending on what you want to happen with -91/10


Rounding is a way of solving an inequality. The way you round depends entirely on the inequality you are trying to solve.

Hmm...

Found it.
https://math.stackexchange.com/a/2421978/478916
Topic archived. No new replies allowed.