notation.cpp

Although not apparent from their appearance, manipulators are functions. Unlike "normal" functions, they are specialized to work with the inserter (<<) and extractor (and >>) operators, respectively. endl (end line) is a familiar manipulator example.

By default, C++ output stream objects, cout for example, display extreme floating-point numbers (those that are large and or near zero) in scientific notation. But programmers can override the output stream's default behavior with manipulators, forcing the stream to display extreme numbers in a fixed-point format (not in scientific notation) and moderate numbers to display with scientific notation.

ProgramOutput
#include <iostream>
using namespace std;

int main()
{
	cout << 23.5 << endl;
	cout << 1000000.0 << endl;
	cout << 6.0221413e23 << endl;
	cout << 1.0 / 30000.0 << endl;

	cout << endl;

	cout << fixed << 23.5 << endl;
	cout << fixed << 1000000.0 << endl;
	cout << fixed << 6.0221413e23 << endl;
	cout << fixed << 1.0 / 30000.0 << endl;

	cout << endl;

	cout << scientific << 23.5 << endl;
	cout << scientific << 1000000.0 << endl;
	cout << scientific << 6.0221413e23 << endl;
	cout << scientific << 1.0 / 30000.0 << endl;

	return 0;
}





23.5
1e+06
6.02214e+23
3.33333e-05



23.500000
1000000.000000
602214130000000022740992.000000
0.000033



2.350000e+01
1.000000e+06
6.022141e+23
3.333333e-05


 
Formatting numbers with manipulators. Two manipulators, fixed and scientific, affect how C++ programs format floating-point numbers. Each manipulator changes the internal configuration of the stream object. So, only the first occurrence of fixed in the second code block and the first occurrence of scientific in the third block is necessary. Nevertheless, I typically include them in all statements for clarity when I use them.

Compare to precision.cpp.