Updated April 4, 2023
Introduction to C++ setprecision() Function
The setprecision is a manipulator function in C++ which is used to sets the decimal precision of floating-point values on output operations. This setprecision is the built-in function defined in <iomanip> header file in C++. The decimal number could contain an infinite length number which requires an infinite memory to store, but the decimal number is to be store in 4 or 8 bytes of memory. Hence some of the digits lost and store only a significant number of digits. Therefore to keep the significant number of digits without loss of information the precision of a floating-point number can be defined. If an inserted or extracted operator is modified the stream object and the concurrent access to the same stream object may create the data race condition. If the stream is in a valid state then an exception is thrown by setprecision() function.
Syntax of C++ setprecision
Below is the declaration of the setprecision() function in C++ is:
setprecision(int n);
Parameters of C++ setprecision Function:
n – n is the int parameter which is specified the floating-point value decimal precision.
The return value of this function is not defined it just used as a manipulator for the stream.
Examples of setprecision() Function in C++
Below are the examples of C++ setprecision:
Example #1
We write the C++ code to understand the setprecision() function more clearly with the following example where we use setprecision() function used to set the different decimal number, as below:
Code:
#include <iostream>
#include <iomanip>
int main ()
{
double dp = 1.5678324;
std::cout << "setprecision(1) : " <<std::setprecision(1) << dp << std::endl;
std::cout << "setprecision(2) : " <<std::setprecision(2) << dp << std::endl;
std::cout << "setprecision(3) : " <<std::setprecision(3) << dp << std::endl;
std::cout << "setprecision(4) : " <<std::setprecision(4) << dp << std::endl;
std::cout << "setprecision(0) : " <<std::setprecision(0) << dp << std::endl;
std::cout << "setprecision(-1) : " <<std::setprecision(-1) << dp << std::endl;
std::cout << "setprecision(-2) : " <<std::setprecision(-2) << dp << std::endl;
return 0;
}
Output:
As in the above code, the setprecision() function is used to set the different precision values for floating-point numbers like 0, 1, 2, 3, 4, -1, and -2. The positive precision numbers are working fine as we can see in the output, but the negative precision numbers are not working as expected it displaying in the default precision number that is 6.
Example #2
We write the C++ code to understand the setprecision() function where we display the default precision number, max precision number and then use the setprecision() function used to set the specific decimal precision number, as below:
Code:
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
int main ()
{
double dp = 1.5678324;
cout<< "The default precision (6) is : " << dp << endl;
cout<< "The max precision is : " << setprecision(std::numeric_limits<double>::digits10 + 1);
cout<< dp << endl;
cout<< "The setprecision (7) is : " << setprecision(7) << dp << endl;
return 0;
}
Output:
As in the above code showing the default precision number, max precision number, and then set the specific precision number by using the setprecision() function. As in the output, we can see that the default precision number value is 6, the max precision number value is 8.
Example #3
We write the C++ code to understand the setprecision() function where we use the setprecision() function to set the specific decimal precision number and use the fixed to setup the decimal precision number fixed specified by the setprecision() function, as below:
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
double dp = 1.567;
setprecision(7);
cout << "The decimal without fixed is : "<< dp << endl;
// Using fixed() function
cout << "The decimal with fixed is : "<< fixed << dp << endl;
return 0;
}
Output:
As in the above code, the setprecision() function is used and set the precision values for the floating-point number to 7. Later in the code, the decimal number is printing which is printing the same as a store with the 4 decimal precision. And then in the code, the fixed-function is used which sets the floating-point values to fixed point notations as here it is set by the setprecision() function to 7, so now when the decimal number is printing, it is printing with the 7 fixed decimal precision.
Example #4
We write the C++ code to understand the setprecision() function where we use the setprecision() function to set the max decimal precision number and use the fixed to set up the max decimal precision number fixed specified by the setprecision() function, as below:
Code:
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
int main ()
{
float fp = 233646.6776;
cout << fp<<endl;
setprecision(std::numeric_limits<float>::digits10 + 1);
cout << fixed<<fp;
return 0;
}
Output:
As in the above code, the setprecision() function is used and set the max precision values for floating-point number. when in the float number is printing it is printing with the default precision that is 4, but the float number is of a large number of precision value. So we can set the fixed precision number to the max by using the setprecision() function to the max, so now when the decimal number is printing, it is printing with all its decimal precision.
Conclusion
The setprecision() function is a built-in function and acts as a manipulator function in C++ which is used to sets the decimal precision of floating-point values on output operations. To use setprecision() function in a program we need to include the <iomanip> header file.
Recommended Article
This is a guide to C++ setprecision. Here we discuss the Introduction to C++ setprecision() Function and its Examples along with Code Implementation and Output. you can also go through our suggested articles to learn more –