Thread: rounding off outputs

  1. #1
    Registered User
    Join Date
    Jan 2005

    rounding off outputs

    I am making this program for a fake store that will take the price of an item and add it to the other items you put in and then multiply by sales tax. I was just wondering how i could round everything off to the 100th. For example, if the price of the item was 1.25 and that was all they bought, 1.25 * .0825 is 1.66562. I would like it to read 1.67. Here is the code so far.

    #include <iostream>
    using namespace std;
    	const float SALESTAX = .0825;
    	float totalprice;
    	float totalpricewithouttax;
    	float item1;
    	float item2;
    	float item3;
    	float item4;
    	float item5;
    	cout<<"Enter the price of an item: ";
    	cin>> item1;
    	if (item1 < 0) {
    		cout<<"You must enter a price";
    		return 1;
    	if (item1 > 0) {
    		cout<<"If this is the only item being bought, enter 0.  If not, enter the price for item 2: ";
    	cin>> item2;
    	if (item2 == 0) {
    		totalpricewithouttax= item1;
    		totalprice= (item1) * (item1 + SALESTAX);
    	cout<< "The price is "<< totalpricewithouttax<< endl;
    	cout<< "The total ammount due is "<< totalprice << endl;

  2. #2
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Aurora CO
    Use the fixed and setprecision() manipuators in your cout. Be sure to include <iomanip>.

    ie... cout << fixed << setprecision(2) << "The price is.....

    fixed will force the output in fixed (vs scientific) format, as well as force the trailing zeros to be displayed. setprecision() sets how many decimal points.
    Last edited by Scribbler; 02-02-2005 at 03:06 PM.

  3. #3
    Registered User
    Join Date
    Oct 2001
    > was just wondering how i could round everything off to the 100th.
    #include <iomanip>
        cout << "The price is "<< fixed << setprecision(2) << totalpricewithouttax<< endl;

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    Or without using the <iomanip> header:

    float fVar;
    cout << fVar;
    Last edited by hk_mp5kpdw; 02-02-2005 at 03:09 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Aug 2003
    Edit: damn! beaten.

  6. #6
    Registered User
    Join Date
    Feb 2005
    You can try something like this. It is crude, but effective.

    roundedNumber = int (fabs(originalNumber) * 100 + 0.5) / 100
    Last edited by bugsmashers; 02-21-2005 at 07:53 PM. Reason: Spelling error

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ rounding
    By dogbert234 in forum C++ Programming
    Replies: 9
    Last Post: 12-23-2005, 04:09 PM
  2. setprecision() - can I count on it rounding or not?
    By major_small in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2005, 02:26 PM
  3. Rounding errors
    By Buckshot in forum C++ Programming
    Replies: 15
    Last Post: 08-16-2005, 09:11 PM
  4. Help with rounding a number
    By nickk in forum C Programming
    Replies: 3
    Last Post: 06-02-2004, 11:44 AM
  5. Rounding numbers
    By OttoDestruct in forum C++ Programming
    Replies: 4
    Last Post: 10-13-2003, 10:24 AM