C++/Data Type/double output
Содержание
- 1 double: Formatting aligns columns, pads blank spaces with "0" character, and controls precision of answer.
- 2 Fixed format with precision of 7
- 3 Scientific format with precision of 7
- 4 Set fixed flag for double value
- 5 Set scientific flag when outputing double
- 6 Set showpos flag for double
- 7 Set the precision to 9
- 8 Set the uppercase flag for double value
double: Formatting aligns columns, pads blank spaces with "0" character, and controls precision of answer.
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
main( )
{
double number,square,sqroot;
cout << "num\t" << "square\t\t" << "square root\n";
number=1.0;
cout.setf(ios::fixed);
for(int i=1;i<16;i++) {
square=number*number;
sqroot=sqrt(number);
cout.fill("0"); // fill blanks with zeros
cout.width(2); // column 2 characters wide
cout.precision(0); // no decimal place
cout << number << "\t";
cout.width(6); // column 6 characters wide
cout.precision(1); // print 1 decimal place
cout << square << "\t\t";
cout.width(8); // column 8 characters wide
cout.precision(6); // print 6 decimal places
cout << sqroot << endl;
number+=1.0;
}
return (0);
}
Fixed format with precision of 7
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << "Default format: " << 123.123456789 << endl;
cout << "Fixed format with precision of 7: ";
cout << setprecision(7) << fixed << 123.123456789 << endl;
return 0;
}
Scientific format with precision of 7
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << "Default format: " << 123.123456789 << endl;
cout << "Scientific format with precision of 7: ";
cout << scientific << 123.123456789 << endl;
return 0;
}
Set fixed flag for double value
#include <iostream>
using namespace std;
int main()
{
int x = 100;
double f = 98.6;
double f2 = 123456.0;
double f3 = 1234567.0;
cout.setf(ios::fixed, ios::floatfield);
cout << "After setting fixed flag:\n";
cout << "f: " << f << " f2: " << f2 << " f3: " << f3 << "\n\n";
return 0;
}
Set scientific flag when outputing double
#include <iostream>
using namespace std;
int main()
{
int x = 100;
double f = 98.6;
double f2 = 123456.0;
double f3 = 1234567.0;
cout.setf(ios::scientific, ios::floatfield);
cout << "After setting scientific flag:\n";
cout << "f: " << f << " f2: " << f2 << " f3: " << f3 << endl;
return 0;
}
Set showpos flag for double
#include <iostream>
using namespace std;
int main()
{
int x = 100;
double f = 98.6;
double f2 = 123456.0;
double f3 = 1234567.0;
// Set the showpos flag.
cout.setf(ios::showpos);
cout << "Setting showpos flag.\n";
cout << "f in default notation after setting showpos: " << f << "\n\n";
return 0;
}
Set the precision to 9
#include <iostream>
using namespace std;
int main()
{
double f = 123456.123456789;
cout << "Using default numeric format.\n";
cout << "f with default precision: " << f << "\n\n";
cout << "Setting the precision to 9.\n";
cout.precision(9);
cout << "f with precision of 9: " << f << "\n\n";
cout << "Switching to fixed-point format.\n";
cout.setf(ios::fixed, ios::floatfield);
cout << "f with precision of 9 in fixed-point: " << f << "\n\n";
// Now, display two decimal places.
cout << "Display two decimal places in all cases: ";
cout.precision(2);
cout << 12.456 << " " << 10.0 << " " << 19.1 << endl;
return 0;
}
Set the uppercase flag for double value
#include <iostream>
using namespace std;
int main()
{
int x = 100;
double f = 98.6;
double f2 = 123456.0;
double f3 = 1234567.0;
// Set the uppercase flag.
cout << "Setting uppercase flag.\n";
cout.setf(ios::uppercase);
cout << "f3 with uppercase flag set: " << f3 << endl;
return 0;
}