Formatting Decimals (float)

This is a discussion on Formatting Decimals (float) within the C++ Programming forums, part of the General Programming Boards category; Can anyone tell me how to set a float to a specific no of decimal places in c++? jim....

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    2

    Formatting Decimals (float)

    Can anyone tell me how to set a float to a specific no of decimal places in c++?

    jim.

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Like this -

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main ()
    {
    	double d = 1.234;
    
    	cout.setf(ios::fixed);
    	cout << setprecision(2) << d; 
    
    	return 0;
    
    }
    zen

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    2
    thanx a lt zen, much obliged. i've been rackin my tiny little brain over this for some time now!

    Thanx again, Jim.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  3. dos game help
    By kwm32 in forum Game Programming
    Replies: 7
    Last Post: 03-28-2004, 05:28 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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