How can I use the COUT to display DOUBLE or FLOAT values?
Thanks
Paul
![]()
This is a discussion on How Do I Cout A Double Or Float within the C++ Programming forums, part of the General Programming Boards category; How can I use the COUT to display DOUBLE or FLOAT values? Thanks Paul...
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 variable#include <iostream.h>
int main()
{
double num = 3.14;
float num2 = 3.201;
cout<<num<<" "<<num2<<endl;
return 0;
}
Paro
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;
I used to be an adventurer like you... then I took an arrow to the knee.