Getting Two Decimal Places

This is a discussion on Getting Two Decimal Places within the C++ Programming forums, part of the General Programming Boards category; Hi i was wondering how to set a float to print out only two decimal places, when it is outputted ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    23

    Getting Two Decimal Places

    Hi i was wondering how to set a float to print out only two decimal places, when it is outputted to the screen.

    The program involves money and you cant have 3 decimals with currency.

    Example
    32.4432
    to
    32.44

    Thanks

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Code:
    #include <iostream>
    #include <iomanip>
    
    int main() 
    {
        double s = 2.3426;
    
        std::cout << setprecision(2); //add this line
        std::cout.setf(ios::showpoint | ios::fixed); // and this line after variables are declared
    
        std::cout << s << std::endl;  //output: 2.34
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal places
    By Gordon in forum Windows Programming
    Replies: 9
    Last Post: 06-08-2008, 10:04 AM
  2. %g doesn't print as many decimal places as %f
    By octoc in forum C Programming
    Replies: 1
    Last Post: 03-31-2008, 01:16 PM
  3. Decimal places
    By Gordon in forum Windows Programming
    Replies: 4
    Last Post: 09-28-2007, 11:03 AM
  4. Decimal places on Floating point number
    By manutdfan in forum C Programming
    Replies: 1
    Last Post: 10-29-2006, 12:56 PM
  5. Replies: 10
    Last Post: 06-12-2002, 04:15 PM

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