Thread: Setprecision

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    1

    Question Setprecision

    How do u set the precision for float variables using Visual C++ 6.0,

    I tried this:

    #include <iostream>
    #include <iomanip>
    .
    .
    using namepsace std;
    .
    .
    float something = 1.111;
    cout << fixed << showpoint;
    cout << setprecision(2) << something;

    this didn't work so i tried this:

    cout(setf(ios::fixed,ios::floatfield);
    cout.setf(ios::showpoint);

    which didn't work either.

    Any suggestions how to get it to work?

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    This works for me on Visual C++ 6.0
    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
        float f = 1.1111;
    
        cout.setf(ios_base::fixed, ios_base::floatfield);
        cout<< setprecision(2) << f <<endl;
    }

  3. #3
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    output:

    Enter float: 7.8

    Variable is formatted two places:
    61.54
    Press any key to continue

    code:


    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
    	float variable;
    
    	cout << "Enter float: ";
    	cin >> variable;
    
    	variable *= 7.89;
    
    	cout << setiosflags(ios::fixed | ios::showpoint | ios::right)
    		 << setprecision(2);
    
    	cout <<"\nVariable is formatted two places: " << endl;
    
    	cout << variable << endl;
    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. setprecision()
    By hallo007 in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2006, 09:38 AM
  2. setprecision??
    By rachael033 in forum C++ Programming
    Replies: 5
    Last Post: 03-22-2006, 02:33 AM
  3. setprecision() - can I count on it rounding or not?
    By major_small in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2005, 02:26 PM
  4. setprecision()
    By OnionKnight in forum C++ Programming
    Replies: 2
    Last Post: 03-04-2005, 09:08 PM
  5. Setw() and setprecision?
    By Furious_George in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2003, 08:20 AM