What is the setprecision function in C++?
The setprecision() function is a built-in function that comes with the iomanip library. As the name suggests, it helps us to set the precision (significant figures/decimal places) of an output.
Significant figures
The function takes in one argument: the number of significant figures required in the output.
#include <iostream>#include <iomanip>using namespace std;int main() {float pi = 3.141592;cout << setprecision(4) << pi;}
Decimal places
To manipulate the decimal places being displayed in the output, add a fixed keyword before the setprecision() function (as shown below). The function will still take in only one argument, but it will be the number of decimal places required in the output.
#include <iostream>#include <iomanip>using namespace std;int main() {float pi = 3.141592;cout << fixed << setprecision(4) << pi;}
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved