The setprecision function in C++ is a powerful tool used to control the number of digits displayed after the decimal point in floating-point numbers. It is part of the <iomanip> library and allows software developers to format output for better readability and precision. By specifying the precision, you can ensure that numbers are displayed exactly as required, whether it's for scientific calculations, financial applications, or any scenario where precision in numerical representation is critical. In this guide, we’ll explore how to use the setprecision function effectively and provide examples of its application in C++ programming.

What is the setprecision Function in C++?

The setprecision function in C++ is a part of the <iomanip> library, which allows you to control the number of significant digits displayed for floating-point numbers when outputting to the console. It manipulates the format of the output to ensure the precision of the numbers is displayed as desired.

The function takes an integer value, n, as a parameter, which specifies the number of significant digits to be displayed. It doesn't round the number itself; instead, it dictates how many digits, including those before and after the decimal point, will be displayed.

Purpose of Setting the Precision for Floating-Point Numbers

In C++, floating-point numbers (like float, double, or long double) are approximations, and their precision can vary due to the limitations of computer memory and representation. Often, these numbers are represented with more digits than are necessary for practical purposes, leading to overly long and unreadable outputs.

The setprecision function helps mitigate this issue by limiting the number of digits shown. This is especially useful in scenarios like:

  • Scientific computations: where you may want to display only the relevant number of digits for clarity.

  • Financial applications: where precision matters, but excessive decimal places are unnecessary.

  • Data analysis: where reducing the precision can make the output cleaner and more understandable.

Basics to Advanced - Learn It All!

Full Stack Java Developer Masters ProgramExplore Program
Basics to Advanced - Learn It All!

How setprecision Impacts the Output of Numbers in C++

The setprecision function directly influences the total number of digits printed for a floating-point number. The effect of setprecision varies depending on whether you're using the default formatting or specific formatting flags, such as fixed or scientific.

1. Default behavior: By default, setprecision controls the total number of significant digits displayed, regardless of the decimal point position.

#include <iostream>
#include <iomanip>
int main() {
double num = 123.456789;
std::cout << std::setprecision(5) << num << std::endl;
// Output: 123.46
return 0;
}

In this example, the setprecision(5) limits the total number of significant digits, rounding off the number to 123.46.

2. Using fixed: When combined with the fixed manipulator, setprecision specifies the number of decimal places (not total significant digits). This is especially useful when you need to control the exact number of decimal places, such as when formatting currency or other precise measurements.

#include <iostream>
#include <iomanip>
int main() {
double num = 123.456789;
std::cout << std::fixed << std::setprecision(2) << num << std::endl;
// Output: 123.46
return 0;
}

In this case, setprecision(2) ensures that exactly two decimal places are printed, and the fixed manipulator ensures that the decimal places are preserved.

Looking to advance your C++ skills? This Advanced C++ Course Online helps you learn advanced C++ concepts like multithreading, data racing deadlocks, and much more, and it is completely free.

How to Use the setprecision Function in C++?

Syntax:

std::setprecision(int n);

The parameter n specifies the number of significant digits to be shown in the output, affecting both the digits before and after the decimal point.

  • When n is set to a value: The setprecision function affects the total number of significant digits shown, not just the digits after the decimal point. For example, if you set n = 5, the number will be rounded or formatted to display exactly 5 significant digits.

  • For numbers greater than 1: setprecision(n) will ensure that the total number of significant digits equals n, including digits before and after the decimal point.

  • For numbers less than 1: The n significant digits will be applied to the fractional part, ensuring the total number of non-zero and zero digits is n.

Examples of Using setprecision in C++

Here are three examples demonstrating the usage of setprecision in C++:

Example 1: Basic Precision Adjustment

In this code, setprecision is used to control the total number of significant digits displayed for a floating-point number.

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double num = 123.456789;
// Set precision to 4 significant digits
cout << "Precision 4: " << setprecision(4) << num << endl;
// Set precision to 6 significant digits
cout << "Precision 6: " << setprecision(6) << num << endl;   
return 0;
}

Output:

Precision 4: 123.5
Precision 6: 123.457

Example 2: Using fixed with setprecision

In this example, setprecision is combined with the fixed manipulator to specify the exact number of decimal places.

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double num = 123.456789;
// Use fixed to control decimal places, set precision to 2 decimal places
cout << fixed << setprecision(2) << num << endl;
// Use fixed to control decimal places, set precision to 4 decimal places
cout << fixed << setprecision(4) << num << endl;
return 0;
}

Output:

123.46
123.4568

Example 3: Using scientific with setprecision

Here, setprecision is used with the scientific manipulator to control the number of significant digits displayed in scientific notation.

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double num = 12345.6789;
// Use scientific notation and set precision to 4 significant digits
cout << scientific << setprecision(4) << num << endl;
// Use scientific notation and set precision to 6 significant digits
cout << scientific << setprecision(6) << num << endl;    
return 0;
}

Output:

1.2346e+04
1.23457e+04

Conclusion

In this article, you have learned an essential 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, along with practical hands-on examples.

To get a better understanding of the C++ programming concepts, you can enroll in the Introduction to C++ SkillUp course.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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. What is the syntax for using the SetPrecision function in C++?

The syntax of the setprecision() function in C++ is : setprecision (int n)

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:

11. How to set precision in C++ for 2 decimal places?

You can set precision for two decimal places in C++ using the std::setprecision(2) function from the <iomanip> header.

SetPrecision_FAQ_2.

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Full Stack Development Program with Generative AI

Cohort Starts: 7 Aug, 2025

20 weeks$4,000
Automation Test Engineer Masters Program

Cohort Starts: 6 Aug, 2025

8 months$1,499
Full Stack Java Developer Masters Program

Cohort Starts: 6 Aug, 2025

7 months$1,449
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 6 Aug, 2025

6 months$1,449