This C++ program demonstrates the use of formatting flags on float output. The formatting flags provided in the header ios_base helps to format the float output. The float output can be either in fixed, scientific or default format. The fixed format has fixed number of significant digits while the default one has the same number of significant digits as the original float value.
Here is the source code of the C++ program which demonstrates the use of formatting flags on float output. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to Demonstrate Use of Formatting Flags on Float Output
*/
#include <iostream>
using namespace std;
int main()
{
float data;
ios_base::fmtflags old;
cout << "Enter a float value :\t";
cin >> data;
old = cout.flags();
cout.setf(ios_base::scientific, ios_base::floatfield);
cout << "\nScientific format :\t" << data << endl;
cout.setf(ios_base::fixed, ios_base::floatfield);
cout << "Fixed-point format :\t" << data << endl;
cout.setf(ios_base::fmtflags(0), ios_base::floatfield);
cout << "Default format :\t" << data << endl;
}
$ a.out Enter a float value : 12.3456 Scientific format : 1.234560e+01 Fixed-point format : 12.345600 Default format : 12.3456
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Related Posts:
- Apply for Computer Science Internship
- Apply for C++ Internship
- Check Programming Books
- Practice Computer Science MCQs
- Check Computer Science Books