Home »
C++ programs »
C++ manipulators programs
C++ program to demonstrate example of setprecision manipulator
Learn about the setprecision manipulator in C++, and demonstrate the use of setprecision manipulator using C++ program.
[Last updated : March 02, 2023]
setprecision Manipulator in C++
The 'setprecision' manipulator is used to control the number of digits of an output stream display of a floating- point value.
This program will demonstrate example of setprecision manipulator in c++ programming language.
setprecision manipulator program in C++
/*C++ program to demonstrate example of setprecision manipulator.*/
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float val=123.456f;
cout << "Value of val with different setprecision parameters:" << endl;
cout << setprecision( 3) << val << endl;
cout << setprecision( 4) << val << endl;
cout << setprecision( 5) << val << endl;
cout << setprecision( 6) << val << endl;
cout << setprecision( 7) << val << endl;
cout << setprecision( 8) << val << endl;
cout << "Left padded values:" << endl;
cout << setw(7) << setfill('0') << setprecision( 3) << val << endl;
cout << setw(7) << setfill('0') << setprecision( 4) << val << endl;
cout << setw(7) << setfill('0') << setprecision( 5) << val << endl;
cout << setw(7) << setfill('0') << setprecision( 6) << val << endl;
cout << setw(7) << setfill('0') << setprecision( 7) << val << endl;
cout << setw(7) << setfill('0') << setprecision( 8) << val << endl;
return 0;
}
Output
Value of val with different setprecision parameters:
123
123.5
123.46
123.456
123.456
123.456
Left padded values:
0000123
00123.5
0123.46
123.456
123.456
123.456