How can I use the COUT to display DOUBLE or FLOAT values?
Thanks
Paul
:)
Printable View
How can I use the COUT to display DOUBLE or FLOAT values?
Thanks
Paul
:)
thats a simple way to do it, you just do it like any other variableQuote:
#include <iostream.h>
int main()
{
double num = 3.14;
float num2 = 3.201;
cout<<num<<" "<<num2<<endl;
return 0;
}
Is there a way to change the display so that I only see 3.1 or 3.2 from your example??
Thanks again.
Paul
look up setprecision() and other stream modifiers in your compilers online help section or the stream section of your favorite textbook/tutorial.
Two ways, either:
Or:Code:#include <iostream>
#include <iomanip>
using namespace std;
...
cout << setiosflags( ios::fixed ) << setprecision( 1 )
<< num << ' ' << num2 << endl;
Code:#include <iostream>
using namespace std;
...
cout.setf( ios::fixed );
cout.precision( 1 );
cout << num << ' ' << num2 << endl;