C++

How to Use Setprecision in C++

You might have learned and studied the floating-point values and the scientific notation in mathematics and physics. You may have also learned to perform round-off on floating-point numbers. The setprecision utility in C++ is often used to alter the sequence of numbers displayed inside a floating-point integer output sequence. It works the same as the round-off functionality. This method is defined in the <iomanip>standard library. In this tutorial, we’ll show you how to use C++’s “setprecision” function. So, let’s get started. We must start the Ubuntu shell application through ā€œCtrl+Alt+Tā€ to work on it. We have to initialize installing the C++ compiler, which is g++ in our case. So, the apt package will be utilized for this purpose so far. The system will install the g++ in just a few seconds:

$ sudo apt install g++

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:

$ g++ new.cc

$ ./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 <iostream>

#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:

$ g++ new.cc

$ ./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 <iostream>

#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:

$ g++ new.cc

$ ./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.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.