How to use Setprecision in C++
Setprecision
Floating-point numerals are arranged via the C++ function known as "Setprecision". You can use this built-in function by importing the iomanip library into your program. By giving the exact number of decimal places, we can use the setprecision function to obtain the desired precise value of a double or floating-point value. The setprecision() function will output n significant digits of the integer without sacrificing any information if an input of size n is supplied.
Syntax
setprecision (int n)
Parameter
n (Required): The number of significant digits needed for a floating-point or double value is specified by this integer-type parameter. The floating-point number with precisely 'n' significant digits will be displayed when you supply a value of 'n' to this option.
Return Value
This Function returns no value. It works exclusively with streams.
One can alter a floating-point value to all of its decimal places or just some of them by using the C++ setprecision function. The following discusses these two cases:
Significant Figures
- Pass the number of significant figures (let's say n) needed as input to the setprecision() function to set the precision in a floating-point
- The amount of significant digits required as the argument n indicates output in this case.
The following example will show you how to print important figures using the C++ setprecision:
#include <iostream> #include <iomanip> using namespace std; int main() { // create a floating-point float num = 8.93128; cout << "Original number is: " << num; cout << "\n"; // print 4 significant figures cout << "The number with 4 significant figures is: "; cout << setprecision(4) <<num; cout << "\n\n"; return 0; }
Output:
Decimal Places
To format a floating-point or double value only—that is, just the decimal places—you can alternatively use the C++ setprecision function. The fixed keyword can be used in place of the setprecision() method to accomplish this. The assortment of decimal places to include in the output is determined by the parameter in the setprecision() method when the fixed keyword is used.
The syntax for printing a large number of decimal places
cout << fixed << setprecision(n) << float_variable
The number of decimal places that must be shown in the output is indicated by the argument n in this case.
The C++ setprecision function with the fixed keyword can be used to output important decimal places, as shown in the example below:
#include <iostream> #include <iomanip> using namespace std; int main() { // initialize a floating-point float num = 7.388; cout << "Original number is: " << num; cout << "\n"; // print 2 significant figures cout << "The number with 2 significant figures is: "; cout << setprecision(3) <<num << endl; // print 2 decimal places cout << "The number with 2 decimal places is: "; cout << fixed << setprecision(2) << num; cout << "\n\n"; return 0; }
Output:
Examples of how to use setprecision in C++
Example 1: Using the setprecision() function
In this example, we'll go over the setprecision() method in detail by setting a double integer with several relevant values:
Code:
#include <iostream> #include <iomanip> #include <ios> using namespace std; int main () { // 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 3: " << setprecision(3); cout << pi << endl; cout << "Setting the precision using" << " setprecision to 5: " << setprecision(5); cout << pi << endl; cout << "Setting the precision using" << " setprecision to 8: " << setprecision(8); cout << pi << endl; cout << "Setting the precision using" << " setprecision to 0: " << setprecision(0); cout << pi << endl; cout << "Setting the precision using" << " setprecision to -3: " << setprecision(-3); cout << pi << endl; cout << "Setting the precision using" << " setprecision to -5: " << setprecision(-5); cout << pi << endl; return 0; }
Output:
Example 2: Maximum Precision and Default Precision
In the following scenario, we will display the highest possible precision and the standard precision and set the precision to a certain significant number using setprecision().
#include <iostream> #include <iomanip> #include <limits> using namespace std; int main () { // establish 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 4 significant digits cout<< "Setting the precision using setprecision() to 4 is : \n" << setprecision(4) << pi << endl; return 0; }
Output: