(C++) How would you go about rounding numbers?

This is a discussion on (C++) How would you go about rounding numbers? within the C++ Programming forums, part of the General Programming Boards category; Ok Basically when I am run the program it will add both numbers 1.67 + 1.32 and give the result ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    3

    (C++) How would you go about rounding numbers?

    Ok Basically when I am run the program it will add both numbers 1.67 + 1.32 and give the result 3.00. Clearly, the sum of the displayed numbers should be 2.99 and not 3.00. The problem is that although the values in a and b have been displayed with two decimal digits, they were added internal to the program as three-digit numbers. The solution is to round the values in a and b before they are added to the statement c = a + b;

    Using the int cast, how would I be able to round the values in variables a and b to the nearest hundredth(penny value) before they are added?

    Code:
    #include <iostream.h>
    #include <iomanip.h>
    void main(void)
    {
       float a, b, c;
    
    a = 1.674;
    b = 1.322;
    cout << setprecision(3) << a << endl;
    cout << setprecision(3) << b << endl;
    cout << "----\n";
    c = a + b;
    cout << setiosflags(ios::showpoint)
         << setprecision(3) << c << endl;
    
    }

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    Here is one way to round using a class and overiding the = operator.


    Code:
    class Math
    {
    public:
    	int x;
    
    	operator int()
    	{
    		return x;
    	}
    
    	int operator=(const double &num)
    	{
    		double y;
    		if ( num < 0 )
    			y=num-.5;
    		else
    			y=num+.5;
    		x=int(y);
    		return x;
    	}
    };
    You would need to change the return to a double for your case. Add or subtrace .005 instead of .5
    Best Regards,

    Bonkey

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072

    Re: (C++) How would you go about rounding numbers?

    Originally posted by jeffcoulter
    The problem is that although the values in a and b have been displayed with two decimal digits, they were added internal to the program as three-digit numbers.
    Forget that.

    There's nothing strange with you program above. 3.00 is the correctly rounded value to 2.998
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    11
    you mean 2.996
    --Cid666

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Yes
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    1

    This may help

    double a, b, c;
    int temp_round;

    a = 1.674;
    b = 1.322;

    cout.setf(ios::fixed);
    cout << setprecision(2) <<"a="<<a<< endl;
    cout <<"b="<<b<< endl;
    cout << "----\n";

    /*
    The following statement multiplies the double identifier named "a" (value of 1.67by 100 resulting in 167.4 and stores the result in an integer variable named temp_round. Due to truncation (float to int) temp_round holds the value of 167 (the .4 is truncated)
    */

    temp_round= int(a*100);

    /*
    The following statement converts the variable temp_round (value of 167) to a double (value of 167.00) and divides it by 100.0 (resulting in 1.67) and stores it back the double identifier named "a"
    */
    a = double(temp_round)/100.0;

    //used the same conversion
    temp_round= int(b*100);
    b = double(temp_round)/100.0;

    c = a + b;

    cout << setprecision(2)<< "c = " <<c<< endl;
    system ("pause");

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Newbie question: rounding numbers
    By cantore in forum C Programming
    Replies: 10
    Last Post: 02-04-2006, 02:24 PM
  3. Rounding numbers
    By OttoDestruct in forum C++ Programming
    Replies: 4
    Last Post: 10-13-2003, 10:24 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 10:15 AM
  5. Rounding Numbers accurately
    By crystaldawn68 in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2001, 02:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21