Thread: Getting Two Decimal Places

  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, 09: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, 12:16 PM
  3. Decimal places
    By Gordon in forum Windows Programming
    Replies: 4
    Last Post: 09-28-2007, 10: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, 03:15 PM