Thread: float formatting using cout

  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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    look at setw and setprecision functions
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  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
    Location
    CA, USA
    Posts
    466
    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-30-2005, 12:35 AM
  4. pin number question
    By melee in forum C Programming
    Replies: 19
    Last Post: 12-11-2004, 01:39 PM
  5. Im stuck....
    By dAzed in forum C++ Programming
    Replies: 8
    Last Post: 10-11-2004, 04:50 PM