iomanip setprecision() function in C++ with Examples Last Updated : 12 Jul, 2025 Comments Improve Suggest changes 27 Likes Like Report The setprecision() method of iomanip library in C++ is used to set the ios library floating point precision based on the precision specified as the parameter to this method. Syntax: setprecision(int n) Parameters: This method accepts n as a parameter which is the integer argument corresponding to which the floating-point precision is to be set. Return Value: This method does not return anything. It only acts as stream manipulators. Example 1: C++ // C++ code to demonstrate // the working of setprecision() function #include <iomanip> #include <ios> #include <iostream> using namespace std; int main() { // Initializing the decimal double num = 3.142857142857; cout << "Before setting the precision: \n" << num << endl; // Using setprecision() cout << "Setting the precision using" << " setprecision to 5: \n" << setprecision(5); cout << num << endl; // Using setprecision() cout << "Setting the precision using" << " setprecision to 9 : \n " << setprecision(9); cout << num << endl; return 0; } Output: Before setting the precision: 3.14286 Setting the precision using setprecision to 5: 3.1429 Setting the precision using setprecision to 9: 3.14285714 Example 2: C++ // C++ code to demonstrate // the working of setprecision() function #include <iomanip> #include <ios> #include <iostream> using namespace std; int main() { // Initializing the decimal double num = 3.14; cout << fixed; cout << "Before setting the precision: \n" << num << endl; // Using setprecision() cout << "Setting the precision using" << " setprecision to 5: \n" << setprecision(5); cout << num << endl; // Using setprecision() cout << "Setting the precision using" << " setprecision to 9: \n" << setprecision(9); cout << num << endl; return 0; } Output: Before setting the precision: 3.140000 Setting the precision using setprecision to 5: 3.14000 Setting the precision using setprecision to 9: 3.140000000 Reference: https://cplusplus.com/reference/iomanip/setprecision/ Create Quiz Comment G guptayashgupta53 Follow 27 Improve G guptayashgupta53 Follow 27 Improve Article Tags : C++ cpp-input-output CPP-Functions cpp-manipulators Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like