Example 01:
So, we have opened the ānew.ccā file with ānanoā instruction. This file is created by using the shellās ātouchā query. The file is now launched in the nano editor as an empty file. We have added the input-output āiostreamā header file at the top. The āiomanipā library has been added to use our codeās setprecision() method. After this, we utilized the standard namespace āstdā to make sure we were using the standard way of code and syntax. The overall code has been performed within the main() function of C++ code. No other user-defined function is used for this purpose.
Within main() function, we have initialized a double type variable āvā with a double value. The first ācoutā standard statement displays the actual double variable value āvā on the shell without any update. After this, we have used 8 cout statements to utilize the setprecision() method in each. This is to apply the setprecision() on the āvā variableās each floating-point every time. You must understand that the setprecision only works on the value greater than or equal to 5. If the floating-point value is larger than 5, it will increment the value before it.
For instance, setprecision() at 1st floating-point will round off ā5ā after the point, and the value ā4ā will be converted to 5. Similarly, the 2nd floating-point value ā2ā cannot be rounded off, the 3rd floating-point value ā7ā will convert the value ā2ā to ā3ā, the 4th floating-point value ā4ā cannot be rounded off, and the 5th floating-point value ā9ā will convert the value ā4ā to 5 before it. At ā0ā point will convert the value ā4ā to 5. The negative setprecision() doesnāt do anything but display the whole actual value. All the values at floating points 0 to 5 and -1, -2 will be displayed after applying the setprecision():
It’s time to compile and run the setprecision C++ code with the g++ compilation query and the ā./a.outā execution query. The output shows that the first setprecision(1) converts 4 to 5. The setprecision(2) did nothing and displays ā4.5ā. The setprecision(3) incremented the value from ā4.52ā to ā4.53ā. The setprecision(4) does nothing to the value ā4.527ā. The setprecision(5) increments the value from ā4.5274ā to ā4.5275ā. The setprecision(0) incremented the value to 5. The setprecision(-1) and setprecision(-2) did nothing as shown below:
$ ./a.out
Example 02:
Letās take a look at another instance. The code is similar to the above example, with only a change in its cout statements. The first cout shows the original values while the next two show the result of setprecision() at floating points 1 and 5. The last cout displays the result of the setprecision() method at floating-point 9, which is physically not available. The 1 and 5 floating-point results are quite expected, but we canāt say anything about floating-point 9. Letās just execute the file and check what will be the output of this code:
#include <iomanip>
using namespace std;
int main () {
double v = 4.52749;
cout <<"Value Before setprecision : " <<v <<'\n';
cout <<setprecision(1) <<"Val at 1: " <<v <<'\n';
cout <<setprecision(5) <<"Val at 5: " <<v <<'\n';
cout <<setprecision(9) <<"Val at 9: " <<v <<'\n';
return 0;
}
After compilation and execution of this code, we have the obvious results for setprecision on locations 1 and 3 of floating-point value ā4.52749ā. The result of setprecision 9 shows the actual value of double variable āvā. This could be due to the fact that the value for location 9 isn’t fixed:
$ ./a.out
Letās just update the code again to fix the values of a variable āvā. So, after the first setprecision() cout statement applied at 1st location of the variable, we have used the fixed variable in cout:
#include <iomanip>
using namespace std;
int main () {
double v = 4.52749;
cout <<"Value Before setprecision : " <<v <<'\n';
cout <<setprecision(1) <<"Val at 1: " <<v <<'\n';
cout <<fixed;
cout <<setprecision(5) <<"Val at 5: " <<v <<'\n';
cout <<setprecision(9) << "Val at 9: " <<v <<'\n';
return 0;
}
After compiling and running this updated code, we have the fixed result of setprecision at location 9 of a variable āvā, i.e., 4.527490000:
$ ./a.out
Conclusion:
Finally, this was all about using the setprecision() method in C++ code to round off and display the value of a double variable. We have also explained fixed variables in the code and their benefits. Plus, we have implemented two important examples to explain the concept of set precision in C++. We hope you found this article helpful. Check out other Linux Hint articles for more tips and tutorials.