### Round value problem

Hi. I need to round any integer value like this-

15 = 20
14 = 10
4 = 4
5 = 5
99 = 100
12345678 = 10000000
44444445 = 50000000
1445 = 2000
446 = 500

Anyone can help me?
Let's define an integer r, which is what you insert into the blank in the following statement: "I want to round to the nearest ___".

The procedure for rounding to the nearest r is simple.

1: Add r/2. When you do an integer division like below, any remainder is truncated. This helps us get around that little issue and lets us do proper rounding (so that 15 would round to 20 and not 10 if you're rounding to the nearest 10).
2: Divide by r. This way, you're discarding all data that is less significant than r, essentially the goal of rounding. Can you see why?
3: Multiply by r to get your rounded integer. You're done!

Can you see why this works? If so, enjoy! If not, let us know.

-Albatross
I dont understand how is working this.
I get the same value while I run the code.
 I get the same value while I run the code.

Could you please show the code you used.
I followed Albatross's method.
1. cin>>r
2. sum = r+(r/2)
3. div = sum/r
4. mul = div*r
5. cout<<mul

(Unfortunately, the code is deleted. However, I used like the above steps in my code)
That's not quite what Albatross said.
 1234567891011 const int r = 10; // "I want to round to the nearest 10 ". int number; cout << "Please enter an integer: " << endl; cin >> number; number += r/2; // 1: Add r/2. number /= r; // 2: Divide by r. number *= r; // 3: Multiply by r cout << number << endl;
oh I see. Misunderstanding..
One little question. That constant value is mandatory, huh?
Anyway, its not the desired output.
12345678 does not comes to 10000000
Well, I made it constant for two reasons. First, it indicates to the reader that I don't intend to change it. Secondly it is telling the compiler that I don't intend to change it. It helps in both respects. But no it isn't mandatory.
Yes. Mandatory.

You have to compute it from the number that you want to round.

For example:
1. number==1445.
=> number has four digits, so r must be 1000

2. number==5
=> only one digit, so no rounding

3. number==15
=> number has two digits, so r must be 10
Another example:
 12345678910111213141516171819202122 #include #include using namespace std; int main() { int data[] = { 15, 14, 4, 5, 99, 12345678, 44444445, 1445, 446 }; const int r = 10; // "I want to round to the nearest 10 ". for (int i=0; i
@kekiverto
U mean r should be changed with the number of digits.
But Its not efficient(specially for my code); since while r=10, it works for 2 digits number; and while r=100, it works for 3 digits number.
I need a common value of r or something another, which works for any case.

@chervil
Bro, U made 12345678 = 12345680; since r=10. But I need 12345678 = 10000000; as u did 14 = 10.
To generalize, have r as a non-constant. Then do.
r = 1;
if(n >= 10 && n < 100)
r = 10;
else if(n >= 100 && n < 1000)
r = 100;
else if(n >= 1000 && n < 10000)
r = 1000;

etc

You can even generalize those else ifs (by making a loop instead) if you want to.
A variation on the previous code:
 1234567891011121314151617181920212223242526272829303132333435 #include #include using namespace std; int round(int n); int main() { int data[] = { 15, 14, 4, 5, 99, -99, -14, -15, 12345678, 44444445, 1445, 446 }; for (int i=0; i

Output:
 15 20 14 10 4 4 5 5 99 100 -99 -90 -14 0 -15 -10 12345678 10000000 44444445 40000000 1445 1000 446 400
Thanks a lot charvil.. :)
Topic archived. No new replies allowed.