Connecting Tech Pros Worldwide Forums | Help | Site Map

about rounding off numbers

Member
 
Join Date: May 2007
Posts: 47
#1: Jul 6 '07
Hi,

I have a small question.

If i need to round off a number to int, i do it like
int j = (int)(4.8);
thus "j" will have value 4;

but my requirement is to see that if the value is 4.8 then "j" should take value as 5 and if the value is 4.3 then "j" should take value as 4.

How can I do this in C++.

Thanks!!

Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,379
#2: Jul 6 '07

re: about rounding off numbers


Use a stringstream:
Expand|Select|Wrap|Line Numbers
  1. int Rounder(double d)
  2. {
  3.     stringstream str;
  4.     str << d;
  5.     int result;
  6.     char decimalpoint;
  7.     int decimal;
  8.     str >> result >> decimalpoint >> decimal;
  9.     if (decimal > 5)
  10.     {
  11.         ++result;
  12.     }
  13.     return result;
  14. }
  15. int main()
  16. {
  17.     double d1 = 4.8;
  18.     cout << Rounder(d1) << endl;
  19.     double d2 = 4.3;
  20.     cout << Rounder(d2) << endl;
  21. }
  22.  
The notion of doing a cast is not rounding. That is covering up a compiler warning about truncation an possible loss of data.

The Rounder() function just reads the double into a stringstream and then extracts it as an int, char , int. Now you can make a test on the decmal portion and decide whether to increment the result or not.

Remember, when you cast in C++ you a) are calling a relic C function with a void* argument or b) your C++ design is faulty.
Member
 
Join Date: May 2007
Posts: 47
#3: Jul 6 '07

re: about rounding off numbers


Thanks for the clear explanation!!
Member
 
Join Date: May 2007
Posts: 47
#4: Jul 6 '07

re: about rounding off numbers


Hi,

the above is applicable only when i have a value between 4 and 5.
But in my program I am computing certain values and those values change each time in the loop. then in such case how can i round off as above??

do we have any in built functions for that??

Thanks!!
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#5: Jul 6 '07

re: about rounding off numbers


Quote:

Originally Posted by flavourofbru

Hi,

the above is applicable only when i have a value between 4 and 5.
...

No it is not limited to that range only.
Member
 
Join Date: Jun 2007
Posts: 67
#6: Jul 6 '07

re: about rounding off numbers


Another option is to add 0.5 onto the number and then floor it, and then cast the result to an int.

if num is a double variable, then this would do it:

Expand|Select|Wrap|Line Numbers
  1. int j = static_cast<int>(floor(num + 0.5));
I believe you'll need to include <cmath> to use the floor function.

this works because the floor function always rounds down, and any number that with a decimal of .5 or higher is raised to the next integer before being rounded. so that with 4.3 is increased to 4.8 and then floored giving you 4; while 4.8 would be raised to 5.3 which is floored to 5.

This is assuming that you want a decimal value of .5 to round up, if you want .5 to round down, then you can do this:

Expand|Select|Wrap|Line Numbers
  1. int j = static_cast<int>(ceil(num - 0.5));
again needing to include <cmath> would be required.
Reply


Similar C / C++ bytes