I tried to use std::setprecision, but with no success.
#include <iostream>
#include <iomanip>
int main()
{
double a = 22 / 7;
std::cout << std::setprecision(5);
std::cout << a;
return 0;
}
The output is 3, instead of 3.14285




Posted 23 January 2009 - 04:45 PM
#include <iostream>
#include <iomanip>
int main()
{
double a = 22 / 7;
std::cout << std::setprecision(5);
std::cout << a;
return 0;
}
Posted 23 January 2009 - 04:51 PM
// use these two lines to set the precision
cout.setf( ios_base::fixed, ios_base::floatfield );
cout.precision( 2 );
cout << "whatever you want here: " << a << endl;
// Reset cout
cout.unsetf( ios_base:: fixed );
cout.precision( 0 );
Posted 23 January 2009 - 05:01 PM
Psionics, on 23 Jan, 2009 - 03:51 PM, said:
...
#include <iostream>
#include <iomanip>
using namespace std;
void setPrec(int prec = -1)
{
if (prec == -1)
{
cout.unsetf(ios_base:: fixed);
cout.precision(0);
}
else
{
cout.setf(ios_base::fixed, ios_base::floatfield);
cout.precision(prec);
}
}
int main()
{
float a = 22 / 7;
setPrec(5);
cout << a;
setPrec();
return 0;
}
This post has been edited by Kirbya: 23 January 2009 - 05:02 PM
Posted 23 January 2009 - 05:11 PM
#include <iostream>
#include <iomanip>
using namespace std;
int main(void)
{
double a = 22 / 7;
cout.setf(ios_base::fixed, ios_base::floatfield); // fixed point
float tub = 25.0f / 3.0f; // This is good to about 6 places
double mint = 10.0 / 3.0; // This one is good to about 15 places
const float million = 1.0e6;
cout << "tub = " << tub << endl;
cout << "mint = " << mint << " and a million mints = ";
cout << million * mint << endl;
cout << "a = " << a << endl;
cout << setprecision(2) << a << "\n\n";
return 0;
}
Posted 23 January 2009 - 05:13 PM
Posted 23 January 2009 - 05:24 PM
// simply change this line double a = 22.0 / 7.0;
This post has been edited by Locke: 23 January 2009 - 05:25 PM
Posted 23 January 2009 - 05:27 PM
This post has been edited by Kirbya: 23 January 2009 - 05:31 PM
Posted 23 January 2009 - 05:51 PM
float f = (float)22/7;
Posted 24 January 2009 - 11:33 PM
|
|
Query failed: connection to localhost:3312 failed (errno=111, msg=Connection refused).
|
