cout

This is a discussion on cout within the C++ Programming forums, part of the General Programming Boards category; Hello, Could someone please tell me how to format data using cout? For example, when using printf if I wanted ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    13

    Question cout

    Hello,

    Could someone please tell me how to format data using cout?

    For example, when using printf if I wanted to print out a floating point number to 2 decimal places I would do:

    printf("%.2f",num);

    Is it possible to do this using cout. The only way I have found is to use sprintf, and give cout the pointer. But surely there is a simpler way.

    Thanks in advance.

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    cout.precision(3);
    cout << 2.3456764 << endl;

    should out : 2.34.

    keep in mind precision specifies the number of significant digits, not the number of decimal places.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class methods to cout stream
    By shintaro in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2008, 06:27 PM
  2. cout vs std::cout
    By none in forum C++ Programming
    Replies: 10
    Last Post: 07-26-2004, 11:20 PM
  3. changing cout output
    By Yohumbus in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2002, 04:09 AM
  4. Redirecting cout stream
    By Arrow Mk84 in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2002, 04:17 PM
  5. printf vs cout
    By RyeDunn in forum C++ Programming
    Replies: 5
    Last Post: 07-09-2002, 04:26 PM

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