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.
1
2
3
4
5
6
7
8
9
10
11
    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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <iomanip>

    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<sizeof(data)/sizeof(data[0]); i++)
    {
        int n = data[i];
        cout << setw(8) << n << "  ";

        n += r/2;                // 1: Add r/2.
        n /= r;                  // 2: Divide by r.
        n *= r;                  // 3: Multiply by r
        cout << setw(8) << n << endl;
    }
}
      15        20
      14        10
       4         0
       5        10
      99       100
12345678  12345680
44444445  44444450
    1445      1450
     446       450
@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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <iomanip>

    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<sizeof(data)/sizeof(data[0]); i++)
    {
        int n = data[i];
        cout << setw(8) << n << "  " << setw(8) << round(n) << endl;
    }
    
}

int round(int n)
{
    // "I want to round to the nearest r ".
    int r = 1;
    
    int temp = n;

    while (temp/=10)
        r *= 10;
        
    n += r/2;                // 1: Add r/2.
    n /= r;                  // 2: Divide by r.
    n *= r;                  // 3: Multiply by r
    
    return n;    
}

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.