C++

How to Use setprecision() in C++?

Are you struggling with formatting floating-point numbers in your C++ code? If so, the setprecision() function may be just what you need. This handy function allows you to specify the precision with which floating-point numbers should be displayed, making it easier to format your output in a clean and professional manner.

This article will explain the setprecision() in C++, covering its syntax and simple examples for better understanding.

What is C++ setprecision() Function

The C++ setprecision() function is used to specify the number of decimal places to be displayed when printing a floating-point value. C++ rounds the decimal places towards a specific value by default, which may not be desirable if you want to display a specific count of decimal places for a specific floating-point value. In such cases, the C++ setprecision() function can be used to ensure that the correct number of decimal places is displayed.

Syntax for setprecision() Function

The syntax of setprecision function is given as:

setprecision(n)

Here, n is the number of decimal places to be displayed.

The <iomanip> header file must be included in a code if you want to use setprecision() function in C++.

How to Use setprecision() Function in C++

There are two methods to use setprecision() function in C++:

Let’s explain how these two methods work.

1: How to Use setprecision() Function with cout

To use the setprecision() function in C++, you can combine it with the cout function, followed by the variable name. This approach allows you to indicate the decimal places number to be shown.

Let’s take a look at an example:

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    double a = 7.89765435678920451;
    cout << "Default is: " << a << endl;
    cout << "setprecision(5) is: " << setprecision(5) << a << endl;
    cout << "setprecision(10) is: " << setprecision(10) << a << endl;
    return 0;
}

We are using a double variable called “a” in this example. The cout function is then used to show a value of “a” with various setprecision() values. We assign the setprecision() to 5 and 10, respectively. The output demonstrates that the decimal places number displayed corresponds to the setprecision() argument.

Output

2: How to Use setprecision() Function with Fixed and Scientific Manipulators

The fixed and scientific manipulators come in handy when using the setprecision() function to format the output of floating-point values in C++. The fixed manipulator allows you to specify the number of decimal places to be displayed after the decimal point, while the scientific manipulator determines the number of significant digits to be displayed.

By using these manipulators with the setprecision() function, you can easily format the output of floating-point values according to your specific needs.

For example:

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    double a = 7.89765435678920451;
    cout <<"Fixed is:"<< fixed << setprecision(2) << a << endl;
    cout <<"Scientific is:"<< scientific << setprecision(4) <<a << endl;
    return 0;
}

The above code demonstrates the use of setprecision() function with fixed and scientific manipulators to display a double variable “a” with four decimal places. The output shows the value of “a” in fixed and scientific notations.

Output

Conclusion

The setprecision() is a useful function in C++ that allows you to format floating-point numbers by specifying the number of decimal places to be displayed. It can be used with cout to format the output of floating-point values according to your specific needs. Additionally, the fixed and scientific manipulators can be used in conjunction with setprecision() to format the output of floating-point values in C++.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.