The C++ setprecision function is used to format floating-point values. This is an inbuilt function and can be used by importing the iomanip library in a program. By using the setprecision function, we can get the desired precise value of a floating-point or a double value by providing the exact number of decimal places. If an argument n is passed to the setprecision() function, then it will give n significant digits of the number without losing any information. 

The setprecision() function is quite useful to prevent loss of information. Let us understand this with the help of an example. Let’s say you have to store the value of pi, which has an infinite number of digits. So, in a 4-bit or an 8-bit memory, some digits will get automatically truncated. This will cause a loss of information. To avoid this, the setprecision() function can be used. For example, setprecision(4) will give 4 significant digits (i.e. 3.142) of the value pi without any loss of information. 

Syntax of C++ Setprecision

The syntax of the setprecision() function in C++ is as follows:

setprecision(argument)

Parameter description

  1. Argument: This is the integer type parameter that specifies the number of significant digits needed in a floating-point or double value. A parameter n will set the floating point to exactly n significant digits.
  2. Return Value: This function does not return a value. It only manipulates streams.

Basics to Advanced - Learn It All!

Caltech PGP Full Stack DevelopmentExplore Program
Basics to Advanced - Learn It All!

Significant Figures and Decimal Places

The C++ setprecision can be used to manipulate a complete floating-point value or just its decimal places. These two cases are discussed below:

  • Significant Figures

To set the precision in a floating-point, simply provide the number of significant figures (say n) required to the setprecision() function as an argument. The function will format the original value to the same number of significant figures (n in this case).

Syntax to print significant figures 

cout << setprecision(n) << float_variable;

Here, argument n is the number of significant figures that need to be displayed as output.

The following example will illustrate how to print significant figures using the C++ setprecision:

#include <iostream>

#include <iomanip>

using namespace std;

int main() {

  // initialize a floating-point 

  float num = 2.71828;

  cout << "Original number is: " << num;

  cout << "\n";

  // print 3 significant figures

  cout << "The number with 3 significant figures is: ";

  cout << setprecision(3) <<num;

  cout << "\n\n";

  return 0; 

}

Cpp_Setprecision_1.

Front or Back-End Development? Learn It All!

Caltech Coding BootcampExplore Program
Front or Back-End Development? Learn It All!
  • Decimal Places

The C++ setprecision can also be used to format only the decimal places instead of the whole floating-point or double value. This can be done using the fixed keyword before the setprecision() method. When the fixed keyword is used, the argument in the setprecision() function specifies the number of decimal places to be printed in the output.

Syntax to print a significant number of decimal places

cout << fixed << setprecision(n) << float_variable

Here, argument n is the number of decimal places that need to be displayed as output.

The following example will illustrate how to print significant decimal places using the C++ setprecision and the fixed keyword:

#include <iostream>

#include <iomanip>

using namespace std;

int main() {

  // initialize a floating-point 

  float num = 2.71828;

  cout << "Original number is: " << num;

  cout << "\n";

  // print 3 significant figures

  cout << "The number with 3 significant figures is: ";

  cout << setprecision(3) <<num << endl;

  // print 3 decimal places

  cout << "The number with 3 decimal places is: ";

  cout << fixed << setprecision(3) << num;

  cout << "\n\n";

  return 0;

}

Cpp_Setprecision_2

Here's How to Land a Top Software Developer Job

Full Stack Development-MEANExplore Program
Here's How to Land a Top Software Developer Job

Examples of  C++ Setprecision Function

Example 1: Using setprecision() Function

In this example, we are going to understand the setprecision() function in a well-explained manner by applying the setprecision() function to set a double value to different significant values:

#include <iostream>

#include <iomanip>

#include <ios>

using namespace std;

int main ()

    //Initialising the pi as double

    double pi = 3.141592653589793238;    

    cout<<"Value of pi before setting the precision: "

        <<pi<<endl; 

    cout << "Setting the precision using"

         << " setprecision to 1: "

         << setprecision(1);

    cout << pi << endl; 

    cout << "Setting the precision using"

         << " setprecision to 2: "

         << setprecision(2);

    cout << pi << endl; 

    cout << "Setting the precision using"

         << " setprecision to 5: "

         << setprecision(5);

    cout << pi << endl; 

    cout << "Setting the precision using"

         << " setprecision to 7: "

         << setprecision(7);

    cout << pi << endl; 

    cout << "Setting the precision using"

         << " setprecision to 0: "

         << setprecision(0);

    cout << pi << endl; 

    cout << "Setting the precision using"

         << " setprecision to -1: "

         << setprecision(-1);

    cout << pi << endl; 

    cout << "Setting the precision using"

         << " setprecision to -3: "

         << setprecision(-3);

    cout << pi << endl; 

    return 0;

}

Cpp_Setprecision_3.

In the above code, we are using the setprecision() function to set the precision up to the different numbers of significant digits of pi. For positive numbers 1, 2, 5, and 7, and for 0, the setprecision() function is working fine and giving the desired output. But for negative numbers -1 and -3, it is displaying the default number of significant digits (i.e. 6).

New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

Example 2: Default Precision and Maximum Precision

In this example, we will display the default precision, maximum precision, and set the precision using setprecision() up to a certain significant digit.

#include <iostream>

#include <iomanip>

#include <limits>

using namespace std;

int main ()

{

//initialise pi as double

double pi = 3.141592653589793238; 

//print default precision

cout<< "The default precision in C++ is : \n" 

    << pi << endl; 

//print maximum precision

cout<< "The max precision in C++ is : \n" 

    << setprecision(numeric_limits<double>::digits10 + 1);

cout<< pi << endl; 

//print precision up to 5 significant digits

cout<< "Setting the precision using setprecision() to 5 is : \n" 

    << setprecision(5) 

    << pi << endl; 

return 0;

}

Cpp_Setprecision_4

In the above code, the pi is printed using three different formats. First is the default precision format using the cout, which prints exactly 6 significant digits and truncates all other digits. The second format is printing the maximum significant digits using numeric_limits<double>::digits10. And the third format is printing 5 significant figures using the setprecision() function.

Learn from the Best in the Industry!

Caltech PGP Full Stack DevelopmentExplore Program
Learn from the Best in the Industry!

Example 3: Using Fixed to Set Precision in Decimal Places.

In the following example, the fixed keyword is used to set precision in the decimal places.

#include <iostream>

#include <iomanip>

#include <ios>

using namespace std;

int main()

{

    //initialise pi as double

    double pi = 3.141;

    cout << fixed;

    cout << "Value of pi before setting the precision: \n"

        << pi << endl;

    // print setprecision(6)

    cout << "Using setprecision() to set"

        << " the precision to 6: \n"

        << setprecision(6); 

    cout << pi << endl; 

    // print setprecision(8)

    cout << "Using setprecision() to set"

        << " the precision to 8: \n"

        << setprecision(8); 

    cout << pi << endl;

    return 0;

}

Cpp_Setprecision_5. 

In the above program, the fixed keyword is used to print the default precision value of the floating-point variable. This displays 6 decimal digits in the output. Since the variable has only 3 decimal digits in the original value, 0 will be printed for the other 3 places. Similarly, when setprecision(8) is used, it prints 8 significant digits with zeros as the remaining digits.

Learn the Ins & Outs of Software Development

Caltech Coding BootcampExplore Program
Learn the Ins & Outs of Software Development

Example 4: Using Fixed to Find Default Precision and Maximum Precision

In this example, the keyword fixed is used to manipulate the default and maximum precision values.

#include <iostream>

#include <iomanip>

#include <limits>

using namespace std;

int main ()

{

    //initialise pi as double

    double pi = 3.141592; 

    cout<<"Printing Default Precision and Max Precision without fixed \n";

    //print default precision

    cout<< "The default precision in C++ is : \n" 

        << pi << endl;    

    //print maximum precision

    cout<< "The max precision in C++ is : \n" 

        << setprecision(numeric_limits<double>::digits10 + 1);

    cout<< pi << endl; 

    cout<<"Printing Default Precision and Max Precision with fixed \n"; 

    cout<<fixed;

    //print default precision

    cout<< "The default precision in C++ is : \n" 

        << pi << endl;    

    //print maximum precision

    cout<< "The max precision in C++ is : \n" 

        << setprecision(numeric_limits<double>::digits10 + 1);

    cout<< pi << endl;    

    return 0;

}

Cpp_Setprecision_6

In the above program, the original values of the floating-point variable are manipulated using the fixed keyword. First, the default and maximum precision values are printed without using the fixed keyword. And then, the default and maximum precision values are printed with the fixed keyword.

Become a Full Stack Developer in 6 Months!

Full Stack Development-MEANExplore Program
Become a Full Stack Developer in 6 Months!

FAQs

1. What is the SetPrecision function in C++? 

SetPrecision is a function in the C++ standard library's <iomanip> header that is used to set the precision (i.e., the number of decimal places) for floating-point values when using the stream insertion operator (<<) with a std::cout stream.

2. How do I use the SetPrecision function? 

To use SetPrecision, include the <iomanip> header and use the function as shown in the following example:

SetPrecision_FAQ_1

3. Can I use SetPrecision with other stream types? 

SetPrecision can be used with any stream type that supports the stream insertion operator (<<). This includes std::cout, std::cerr, and std::ofstream, among others.

4. Does SetPrecision round the value or truncate it?

The SetPrecision function truncates the value, it does not round it.

5. How to set precision for a particular variable rather than the entire stream? 

You can chain the setprecision function with the variable you want to set precision to.

std::cout<<std::setprecision(5)<<value;

You can also use the manipulator fixed with setprecision to set precision for a particular variable.

std::cout << std::fixed << std::setprecision(5) << value;

6. How can I get the current precision setting of a stream? 

You can use the precision() member function of the stream to get the current precision setting.

std::cout << std::cout.precision();

7. Is it possible to set a global precision for all floating-point values in a program? 

Yes, it is possible to set a global precision for all floating-point values in a program by using the unsetf function to remove the existing precision setting and then using setprecision to set a new one. For example:

std::cout.unsetf(std::ios::floatfield);

std::cout << std::setprecision(5);

This will set a global precision of 5 decimal places for all floating-point values used in the program.

8. Can I use the SetPrecision function with user-defined types?

To use the SetPrecision function with user-defined types, you will need to overload the stream insertion operator (<<) and include the necessary code to handle the precision setting. You can also overload the setprecision function for your user-defined type.

9. Can I set precision for a fixed-point notation? 

Yes, you can set precision for fixed-point notation by using the fixed manipulator in combination with setprecision. This will ensure that the number is displayed in fixed-point notation with the specified number of decimal places. For example:

std::cout << std::fixed << std::setprecision(5) << value << std::endl;

10. Is it possible to set the precision for a numeric string? 

Yes, it is possible to set the precision for a numeric string by using a combination of stringstream and setprecision. You can use stringstream to convert a numeric value to a string and then use setprecision to set the precision of the string. For example:

SetPrecision_FAQ_2.

Master front-end and back-end technologies and advanced aspects in our Post Graduate Program in Full Stack Web Development. Unleash your career as an expert full stack developer. Get in touch with us NOW!

Conclusion

In this article, you have learned an important function from the iomanip library called setprecision. You can use this function to specify the number of significant digits in a floating point number that you want to be returned. You saw multiple use-cases of the setprecision function with practical hands-on examples.

To get a better understanding of the C++ programming concepts, you can walk through our tutorial on C++ Programming for Beginners.

To learn more about the important aspects and technologies used in software development, you can opt for the Full Stack Web Development course provided by Simplilearn. . Through this course, you will learn the end-to-end technologies used in professional software development such as DevOps, Agility, Java, CSS, HTML, AWS, etc.

We also recommend you to check out our complete list of free online courses. If you have any queries or suggestions for us, please mention them in the comment box and our experts will answer them for you as soon as possible.

Happy Learning!

About the Author

Ravikiran A SRavikiran A S

Ravikiran A S works with Simplilearn as a Research Analyst. He an enthusiastic geek always in the hunt to learn the latest technologies. He is proficient with Java Programming Language, Big Data, and powerful Big Data Frameworks like Apache Hadoop and Apache Spark.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.
  • *According to Simplilearn survey conducted and subject to terms & conditions with Ernst & Young LLP (EY) as Process Advisors