C + + formatted output floating-point numbers
Copy Code code as follows:
#include <iostream>
Using Std::cout;
Using Std::endl;
Using Std::fixed;
Using Std::scientific;
int main ()
{
Double x = 0.001234567;
Double y = 1.946e9;
cout << "displayed in default format:" << endl << x << '/t ' << y << Endl;
cout << "/ndisplayed in scientific format:" << Endl << scientific << x << '/t ' << y &L t;< Endl;
cout << "/ndisplayed in fixed format:" << endl << fixed << x << '/t ' << y << end L
return 0;
}
Displayed in default format:
0.00123457 1.946e+009
displayed in scientific format:
1.234567e-003 1.946000e+009
displayed in fixed format:
0.001235 1946000000.000000
Copy Code code as follows:
#include <iostream.h>
Main (void)
{
float a=100100.0, b=0.08;
COUT.SETF (Ios::right|ios::scientific|ios::showpoint);
Cout.width (20);
cout << (-A*B);
return 0;
}
-8.008000e+003
Copy Code code as follows:
#include <iostream>
#include <iomanip>
#include <limits>
Using Std::cout;
Using Std::endl;
Using Std::setprecision;
Using Std::numeric_limits;
int main () {
Const double PI = 3.14;
cout << Endl;
for (double radius =. 2; Radius <= 3.0; radius = 2)
cout << "radius ="
<< setprecision (numeric_limits<double>::d IGITS10 + 1)
<< std::scientific << radius<< "area ="
<< STD::SETW (a) << setprecision (6) << std::fixed << pi * radius * radi
US << Endl;
return 0;
}
Radius = 2.0000000000000001e-001 Area = 0.125600
Radius = 4.0000000000000002e-001 Area = 0.502400
Radius = 6.0000000000000009e-001 Area = 1.130400
Radius = 8.0000000000000004e-001 Area = 2.009600
Radius = 1.0000000000000000e+000 Area = 3.140000
Radius = 1.2000000000000000e+000 Area = 4.521600
Radius = 1.3999999999999999e+000 Area = 6.154400
Radius = 1.5999999999999999e+000 Area = 8.038400
Radius = 1.7999999999999998e+000 Area = 10.173600
Radius = 1.9999999999999998e+000 Area = 12.560000
Radius = 2.1999999999999997e+000 Area = 15.197600
Radius = 2.3999999999999999e+000 Area = 18.086400
Radius = 2.6000000000000001e+000 Area = 21.226400
Radius = 2.8000000000000003e+000 Area = 24.617600
Copy Code code as follows:
#include <iostream>
#include <iomanip>
#include <string>
using namespace Std;
int main () {
Ios_base::fmtflags flags = Cout.flags ();
Double pi = 3.14285714;
cout << "PI =" << setprecision (5) << pi << '/n ';
Cout.flags (flags);
}
PI = 3.1429
Copy Code code as follows:
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace Std;
int main ()
{
Double Root2 = sqrt (2.0);
int places;
cout << setiosflags (ios::fixed)
<< "Square root of 2 with precisions 0-9./n"
<< "Precision set by"
<< "Precision member function:" << Endl;
for (places = 0; places <= 9; places++) {
Cout.precision (places);
cout << root2 << '/n ';
}
cout << "/nprecision set by"
<< "Setprecision manipulator:/n";
for (places = 0; places <= 9; places++)
cout << setprecision (places) << root2 << '/n ';
return 0;
}