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.