setprecision()

This is a discussion on setprecision() within the C++ Programming forums, part of the General Programming Boards category; I have been tricked! setprecision() does not change the amount of decimals being printed out, it changes the amount of ...

  1. #1
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555

    setprecision()

    I have been tricked! setprecision() does not change the amount of decimals being printed out, it changes the amount of numbers to be printed out. E.g:
    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
    	cout<<setprecision(5)<<24.435166;
    	cin.get();
    	return 0;
    }
    Would output 24.435 (5 numbers) instead of 24.43517 (5 decimals).
    printf() can do this simply by using %.5f, so how do you do it with cout?

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    before the cout add:
    cout.setf(ios::fixed);

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >setprecision() does not change the amount of decimals being printed out
    As Thantos demonstrated, it does if you use fixed point:
    Code:
    	cout << fixed << setprecision(5) << 24.435166;

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. Setw() and setprecision?
    By Furious_George in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2003, 09:20 AM
  5. setprecision() Help
    By frgmstr in forum C++ Programming
    Replies: 2
    Last Post: 04-15-2002, 03:24 PM

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