how to print fraction inc++

This is a discussion on how to print fraction inc++ within the C++ Programming forums, part of the General Programming Boards category; like in c we can print a fraction like #include<stdio.h> void main() { float f=21.3456; int i=2; printf("%*f",i,f); } which ...

  1. #1
    deeps
    Guest

    how to print fraction inc++

    like in c we can print a fraction like
    #include<stdio.h>
    void main()
    {
    float f=21.3456;
    int i=2;
    printf("%*f",i,f);
    }
    which will print 21.34
    how can we do the same with cout<<

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Code:
    #include <iomanip>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        float fValue = 21.3456;
    
        cout << setiosflags( ios::fixed) << setprecision(2) << fValue << endl;
    
        return 0;
    }
    Or, if you don't want to include the <iomanip> header, you can do this:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        float fValue = 21.3456;
    
        cout.setf( ios::fixed );
        cout.precision( 2 );
        cout << fValue << endl;
    
        return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM
  2. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  3. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 09:13 PM
  4. Overloading insertion and extraction problem
    By Curwa in forum C++ Programming
    Replies: 1
    Last Post: 01-15-2003, 09:20 PM
  5. reducing a fraction
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-13-2002, 08:56 PM

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