Thread: Floating points and Double output

  1. #1
    Registered User
    Join Date
    Aug 2001
    Location
    Fort Worth, TX
    Posts
    53

    Question Floating points and Double output

    I lost some code that I had for outputting the, decimal and trailing zeros(instead of scientific format). I am outputting to a file and using ofsream for my outout.

    I tried ofstream::showpoint, but had no sucess. This is a simple problem and do not have my books with me and am looking for a quick solution thanks to all.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    cout << setiosflags(ios::fixed) << setprecision(2) << x;
    setiosflags(ios::fixed) sets the decimal, and setprecision() sets the number of digits to the right of the decimal. Cout.precision(); will set precision as well.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    20
    HI,
    I think you are looking for a way to show the decimal and trailing zeros.

    You need

    cout.setf (ios::fixed | ios :: showpoint);
    cout.precision (2);

    I just studied for a test on this very thing today. When you set fixed along with precision the precision refers to the number of decimals following the decimal point and it rounds up too.

    the above way is using the member functions. You can also do in as a stream manipulator

    cout << setprecision(2)
    << setiosflags ( ios::fixed | ios::showpoint )
    << your_floating_point_number_goes_here
    << endl;

    you might also have to unset a flag and you can do that by
    cout.unsetf ( ios::fixed | ios::showpoint);
    and that turns them off.

    I studied this for quite a while and the instructor did not ask this question. I am glad that someone did today!

    good luck,
    susan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 11-03-2008, 09:48 PM
  2. For the numerical recipes in C types!
    By Smattacus in forum C Programming
    Replies: 5
    Last Post: 10-28-2008, 07:57 PM
  3. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  4. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM
  5. expected primary expression
    By mju4t in forum C Programming
    Replies: 2
    Last Post: 03-27-2007, 06:59 PM