Thread: very amateur question:

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    7

    very amateur question:

    How do i get a number to display only two decimal points? I'm using a float variable, and i know that it's default is 6 decimal spots. Is there a way to change this? or a new variable type?

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    float f = 0.333;
    cout.setprecision( 2 );
    cout<<f<<endl; //Should output 0.33
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    7
    i get the following error:

    error C2039: 'setprecision' : is not a member of 'ostream_withassign'
    i've inly included the following:
    #include <iostream.h>
    #include <string.h>
    #include <stdlib.h>

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    7
    aha, i got it, its "precision" w/o the "set". thanks.

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Try this...
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    ...
    ...
    cout << setprecision(2);
    ... this should work. setprecision() is the ANSI standard manipulator. I suspect it failed in your setup because you are using the old style headers, (those with .h on them), and not the standard compliant ones declared in namespace std.

    If you try this and it fails, tell me what compiler you are using.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>#include <iomanip>
    This was the missing item causing the problem. It's where setprecision lives.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Amateur question.
    By RP319 in forum C++ Programming
    Replies: 11
    Last Post: 02-07-2005, 07:27 AM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM