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.