How Do I Cout A Double Or Float

This is a discussion on How Do I Cout A Double Or Float within the C++ Programming forums, part of the General Programming Boards category; How can I use the COUT to display DOUBLE or FLOAT values? Thanks Paul...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    2

    How Do I Cout A Double Or Float

    How can I use the COUT to display DOUBLE or FLOAT values?

    Thanks

    Paul


  2. #2
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160
    #include <iostream.h>

    int main()
    {
    double num = 3.14;
    float num2 = 3.201;

    cout<<num<<" "<<num2<<endl;
    return 0;
    }
    thats a simple way to do it, you just do it like any other variable
    Paro

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    2
    Is there a way to change the display so that I only see 3.1 or 3.2 from your example??

    Thanks again.

    Paul

  4. #4
    Unregistered
    Guest
    look up setprecision() and other stream modifiers in your compilers online help section or the stream section of your favorite textbook/tutorial.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,674
    Two ways, either:

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
        ...
    
        cout << setiosflags( ios::fixed ) << setprecision( 1 )
             << num << ' ' << num2 << endl;
    Or:

    Code:
    #include <iostream>
    using namespace std;
    
        ...
    
        cout.setf( ios::fixed );
        cout.precision( 1 );
        cout << num << ' ' << num2 << endl;
    I used to be an adventurer like you... then I took an arrow to the knee.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  3. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  4. Configurations give different results
    By Hubas in forum Windows Programming
    Replies: 2
    Last Post: 04-11-2003, 11:43 AM
  5. error declaration terminated incorrectly help
    By belfour in forum C++ Programming
    Replies: 7
    Last Post: 11-25-2002, 08:07 PM

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