Thread: formating display

  1. #1
    Registered User tio1225's Avatar
    Join Date
    Sep 2005
    Posts
    6

    Unhappy formating display

    hi i need to display a floating point variable with two deciaml places. my problem is that when my answer is say 45.50 the zero does not show. how can i get the zero to show.

    Code:
    #include <iostream.h>
    #include <iomanip.h>               
    #include <math.h>                 
    int main()
    {
        float lenght, width, height, paintcost, gallonsneeded, area, cost;
        
        lenght = 20;
        height = 8;
        width = 12;
        paintcost = 22.75;
        area = 2*(lenght*height) + 2*(width*height) + (lenght*width);
        gallonsneeded = area/440;
        gallonsneeded = ceil(gallonsneeded);    
        cost = gallonsneeded * 22.75;         
        cout << " Gallons of paint needed will be  " << gallonsneeded << endl;
        cout << " Total painting coast will be    "  << setprecision(4) << cost << endl;
        system("pause");
        return 0;
    }
    Last edited by tio1225; 09-10-2005 at 06:45 PM.

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Code:
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    Where 2 = how many places after decimal. Once declared it will effect all until changed again. If you wish to change it again, you only need to do cout.precision(#);
    Last edited by Enahs; 09-10-2005 at 07:20 PM.

  3. #3
    Registered User tio1225's Avatar
    Join Date
    Sep 2005
    Posts
    6
    do i need to include a new header for this to work

  4. #4
    Registered User tio1225's Avatar
    Join Date
    Sep 2005
    Posts
    6
    forget my last messge it works thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. display character size...(quite urgent..)
    By karthi in forum C Programming
    Replies: 10
    Last Post: 07-11-2007, 09:42 PM
  2. new problem with class
    By jrb47 in forum C++ Programming
    Replies: 0
    Last Post: 12-01-2006, 08:39 AM
  3. My text doesn't display
    By joeprogrammer in forum Game Programming
    Replies: 11
    Last Post: 02-23-2006, 10:01 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM