iomanip setpricision() function in C++ with Examples
The setprecision() method of iomaip 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 returns anything. It only acts as stream manipulators.
Example 1:
// 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; } |
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++ 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; } |
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/
Recommended Posts:
- iomanip setfill() function in C++ with Examples
- iomanip setiosflags() function in C++ with Examples
- iomanip setw() function in C++ with Examples
- iomanip resetiosflags() function in C++ with Examples
- iomanip setbase() function in C++ with Examples
- ios bad() function in C++ with Examples
- ios eof() function in C++ with Examples
- btowc() function in C/C++ with Examples
- ios setstate() function in C++ with Examples
- wcscpy() function in C++ with Examples
- wcscmp() function in C++ with Examples
- fill() function in C++ STL with examples
- fill_n() function in C++ STL with examples
- ios operator() function in C++11 with Examples
- ios operator !() function in C++ with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



