float formatting using cout

This is a discussion on float formatting using cout within the C++ Programming forums, part of the General Programming Boards category; Hey all, I just want to simply format the number of decimal places displayed when using std::cout. If you know ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    102

    float formatting using cout

    Hey all, I just want to simply format the number of decimal places displayed when using std::cout.
    If you know how please post.
    My Favorite Programming Line:
    Code:
    #define true ((rand() % 2) ? true : false)

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    look at setw and setprecision functions
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Without showpoint, setprecision sets the number of digits to display (significant figures).
    With showpoint, setprecision sets the number of digits after the decimal to display.
    Don't quote me on that... ...seriously

  4. #4
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    307
    Code:
    #include <iostream>
    #include <iomanip>
    
    int main()
    {
      double money = 15.75;
      std::cout << std::fixed << std::setprecision (2) << money << std::endl;
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  2. help me
    By warthog89 in forum C Programming
    Replies: 11
    Last Post: 09-30-2006, 08:17 AM
  3. My attempt at lighting math
    By psychopath in forum Game Programming
    Replies: 11
    Last Post: 03-29-2005, 11:35 PM
  4. pin number question
    By melee in forum C Programming
    Replies: 19
    Last Post: 12-11-2004, 12:39 PM
  5. Im stuck....
    By dAzed in forum C++ Programming
    Replies: 8
    Last Post: 10-11-2004, 04:50 PM

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