Fixed form, Scientific form and Precision setting for floating point numbers

Sometimes we want to output the floating point numbers in fixed or scientific form and sometimes with fixed precision so as to align all the output. The following program is an example to demonstrate that:



//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//This example shows how to output floating point numbersin fixed and
#include<iostream>

using namespace
std;

int
main()
{

float
number = 123.456;

cout << "number in fixed form = " << number << endl; //default is fixed
cout << "number in scientific form = " << scientific << number << endl;
cout.precision(2);
cout << "number in fixed form with precision 2 = " << fixed << number << endl; //here the format was scientific
cout.precision(3);
cout << "number in fixed form with precision 3 = " << number << endl;
cout.precision(4);
cout << "number in fixed form with precision 4 = " << number << endl;
cout.precision(5);
cout << "number in fixed form with precision 5 = " << number << endl;

return
0;
}






The output is as follows:

Check out this stream