[try Beta version]
Not logged in

 
Round value problem

Oct 31, 2013 at 8:16pm
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?
Oct 31, 2013 at 8:24pm
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
Oct 31, 2013 at 9:28pm
I dont understand how is working this.
I get the same value while I run the code.
Nov 1, 2013 at 8:29am
I get the same value while I run the code.

Could you please show the code you used.
Nov 1, 2013 at 8:41am
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)
Nov 1, 2013 at 9:30am
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;
Nov 1, 2013 at 9:43am
oh I see. Misunderstanding..
One little question. That constant value is mandatory, huh?
Nov 1, 2013 at 10:01am
Anyway, its not the desired output.
12345678 does not comes to 10000000
Nov 1, 2013 at 10:01am
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.
Nov 1, 2013 at 10:06am
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
Nov 1, 2013 at 10:13am
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
Nov 1, 2013 at 10:49am
@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.
Nov 1, 2013 at 10:58am
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.
Nov 1, 2013 at 11:00am
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

Nov 1, 2013 at 1:58pm
Thanks a lot charvil.. :)
Topic archived. No new replies allowed.