iomanip setprecision() function in C++ with Examples Last Updated : 18 Feb, 2022 Comments Improve Suggest changes Like Article 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: http://www.cplusplus.com/reference/iomanip/setprecision/ Comment More infoAdvertise with us Next Article iomanip setprecision() function in C++ with Examples G guptayashgupta53 Follow Improve Article Tags : C++ cpp-input-output CPP-Functions cpp-manipulators Practice Tags : CPP Similar Reads iomanip setbase() function in C++ with Examples The setbase() method of iomanip library in C++ is used to set the ios library basefield flag based on the argument specified as the parameter to this method. Syntax: setbase (int base) Parameters: This method accepts base as a parameter which is the integer argument corresponding to which the base i 2 min read iomanip setfill() function in C++ with Examples The setfill() method of iomanip library in C++ is used to set the ios library fill character based on the character specified as the parameter to this method. Syntax: setfill(char c) Parameters: This method accepts c as a parameter which is the character argument corresponding to which the fill is t 2 min read setw() function in C++ with Examples The setw() method of iomanip library in C++ is used to set the ios library field width based on the width specified as the parameter to this method. The setw() stands for set width and it works for both the input and the output streams.Syntaxsetw(int n);Parameters:n: It is the integer argument corre 2 min read norm() function in C++ with Examples The norm() function is defined in the complex header file. This function is used to return the squared magnitude of the complex number z. Syntax: template<class T> T norm (const complex<T>& z); Parameter: z: It represents the given complex number. Return: It returns the squared magni 1 min read ios manipulators fixed() function in C++ The fixed() method of stream manipulators in C++ is used to set the floatfield format flag for the specified str stream. This flag sets the floatfield to fixed. It means that the floating-point values will be written in fixed point notations. Syntax: ios_base& fixed (ios_base& str) Parameter 2 min read Like