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<<
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<<
Or, if you don't want to include the <iomanip> header, you can do this:Code:#include <iomanip> #include <iostream> using namespace std; int main() { float fValue = 21.3456; cout << setiosflags( ios::fixed) << setprecision(2) << fValue << endl; return 0; }
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