The C++ setprecision function is used to format floating-point values. This is an inbuilt function and can be used by importing the iomanip library in a program. By using the setprecision function, we can get the desired precise value of a floating-point or a double value by providing the exact number of decimal places. If an argument n is passed to the setprecision() function, then it will give n significant digits of the number without losing any information.
The setprecision() function is quite useful to prevent loss of information. Let us understand this with the help of an example. Let’s say you have to store the value of pi, which has an infinite number of digits. So, in a 4-bit or an 8-bit memory, some digits will get automatically truncated. This will cause a loss of information. To avoid this, the setprecision() function can be used. For example, setprecision(4) will give 4 significant digits (i.e. 3.142) of the value pi without any loss of information.
Syntax of C++ Setprecision
The syntax of the setprecision() function in C++ is as follows:
setprecision(argument)
Parameter description
- Argument: This is the integer type parameter that specifies the number of significant digits needed in a floating-point or double value. A parameter n will set the floating-point to exactly n significant digits.
- Return Value: This function does not return a value. It only manipulates streams.
Significant Figures and Decimal Places
The C++ setprecision can be used to manipulate a complete floating-point value or just its decimal places. These two cases are discussed below:
-
Significant Figures
To set the precision in a floating-point, simply provide the number of significant figures (say n) required to the setprecision() function as an argument. The function will format the original value to the same number of significant figures (n in this case).
Syntax to print significant figures
cout << setprecision(n) << float_variable;
Here, argument n is the number of significant figures that need to be displayed as output.
The following example will illustrate how to print significant figures using the C++ setprecision:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
// initialize a floating-point
float num = 2.71828;
cout << "Original number is: " << num;
cout << "\n";
// print 3 significant figures
cout << "The number with 3 significant figures is: ";
cout << setprecision(3) <<num;
cout << "\n\n";
return 0;
}
-
Decimal Places
The C++ setprecision can also be used to format only the decimal places instead of the whole floating-point or double value. This can be done using the fixed keyword before the setprecision() method. When the fixed keyword is used, the argument in the setprecision() function specifies the number of decimal places to be printed in the output.
Syntax to print a significant number of decimal places
cout << fixed << setprecision(n) << float_variable
Here, argument n is the number of decimal places that need to be displayed as output.
The following example will illustrate how to print significant decimal places using the C++ setprecision and the fixed keyword:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
// initialize a floating-point
float num = 2.71828;
cout << "Original number is: " << num;
cout << "\n";
// print 3 significant figures
cout << "The number with 3 significant figures is: ";
cout << setprecision(3) <<num << endl;
// print 3 decimal places
cout << "The number with 3 decimal places is: ";
cout << fixed << setprecision(3) << num;
cout << "\n\n";
return 0;
}
Examples of C++ Setprecision Function
Example 1: Using setprecision() Function
In this example, we are going to understand the setprecision() function in a well-explained manner by applying the setprecision() function to set a double value to different significant values:
#include <iostream>
#include <iomanip>
#include <ios>
using namespace std;
int main ()
{
//Initialising the pi as double
double pi = 3.141592653589793238;
cout<<"Value of pi before setting the precision: "
<<pi<<endl;
cout << "Setting the precision using"
<< " setprecision to 1: "
<< setprecision(1);
cout << pi << endl;
cout << "Setting the precision using"
<< " setprecision to 2: "
<< setprecision(2);
cout << pi << endl;
cout << "Setting the precision using"
<< " setprecision to 5: "
<< setprecision(5);
cout << pi << endl;
cout << "Setting the precision using"
<< " setprecision to 7: "
<< setprecision(7);
cout << pi << endl;
cout << "Setting the precision using"
<< " setprecision to 0: "
<< setprecision(0);
cout << pi << endl;
cout << "Setting the precision using"
<< " setprecision to -1: "
<< setprecision(-1);
cout << pi << endl;
cout << "Setting the precision using"
<< " setprecision to -3: "
<< setprecision(-3);
cout << pi << endl;
return 0;
}
In the above code, we are using the setprecision() function to set the precision up to the different numbers of significant digits of pi. For positive numbers 1, 2, 5, and 7, and for 0, the setprecision() function is working fine and giving the desired output. But for negative numbers -1 and -3, it is displaying the default number of significant digits (i.e. 6).
Example 2: Default Precision and Maximum Precision
In this example, we will display the default precision, maximum precision, and set the precision using setprecision() up to a certain significant digit.
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
int main ()
{
//initialise pi as double
double pi = 3.141592653589793238;
//print default precision
cout<< "The default precision in C++ is : \n"
<< pi << endl;
//print maximum precision
cout<< "The max precision in C++ is : \n"
<< setprecision(numeric_limits<double>::digits10 + 1);
cout<< pi << endl;
//print precision up to 5 significant digits
cout<< "Setting the precision using setprecision() to 5 is : \n"
<< setprecision(5)
<< pi << endl;
return 0;
}
In the above code, the pi is printed using three different formats. First is the default precision format using the cout, which prints exactly 6 significant digits and truncates all other digits. The second format is printing the maximum significant digits using numeric_limits<double>::digits10. And the third format is printing 5 significant figures using the setprecision() function.
Example 3: Using Fixed to Set Precision in Decimal Places.
In the following example, the fixed keyword is used to set precision in the decimal places.
#include <iostream>
#include <iomanip>
#include <ios>
using namespace std;
int main()
{
//initialise pi as double
double pi = 3.141;
cout << fixed;
cout << "Value of pi before setting the precision: \n"
<< pi << endl;
// print setprecision(6)
cout << "Using setprecision() to set"
<< " the precision to 6: \n"
<< setprecision(6);
cout << pi << endl;
// print setprecision(8)
cout << "Using setprecision() to set"
<< " the precision to 8: \n"
<< setprecision(8);
cout << pi << endl;
return 0;
}
In the above program, the fixed keyword is used to print the default precision value of the floating-point variable. This displays 6 decimal digits in the output. Since the variable has only 3 decimal digits in the original value, 0 will be printed for the other 3 places. Similarly, when setprecision(8) is used, it prints 8 significant digits with zeros as the remaining digits.
Example 4: Using Fixed to Find Default Precision and Maximum Precision
In this example, the keyword fixed is used to manipulate the default and maximum precision values.
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
int main ()
{
//initialise pi as double
double pi = 3.141592;
cout<<"Printing Default Precision and Max Precision without fixed \n";
//print default precision
cout<< "The default precision in C++ is : \n"
<< pi << endl;
//print maximum precision
cout<< "The max precision in C++ is : \n"
<< setprecision(numeric_limits<double>::digits10 + 1);
cout<< pi << endl;
cout<<"Printing Default Precision and Max Precision with fixed \n";
cout<<fixed;
//print default precision
cout<< "The default precision in C++ is : \n"
<< pi << endl;
//print maximum precision
cout<< "The max precision in C++ is : \n"
<< setprecision(numeric_limits<double>::digits10 + 1);
cout<< pi << endl;
return 0;
}
In the above program, the original values of the floating-point variable are manipulated using the fixed keyword. First, the default and maximum precision values are printed without using the fixed keyword. And then, the default and maximum precision values are printed with the fixed keyword.
Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!
Conclusion
In this article, you have learned an important function from the iomanip library called setprecision. You can use this function to specify the number of significant digits in a floating point number that you want to be returned. You saw multiple use-cases of the setprecision function with practical hands-on examples.
To get a better understanding of the C++ programming concepts, you can walk through our tutorial on C++ Programming for Beginners.
To learn more about the important aspects and technologies used in software development, you can opt for the Full Stack Web Development course provided by Simplilearn. . Through this course, you will learn the end-to-end technologies used in professional software development such as DevOps, Agility, Java, CSS, HTML, AWS, etc.
We also recommend you to check out our complete list of free online courses. If you have any queries or suggestions for us, please mention them in the comment box and our experts will answer them for you as soon as possible.
Happy Learning!