Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Set precision in C++??
3.
Syntax of C++ setprecision
4.
How SetPrecision in C++ Works?
5.
Parameters of C++ setprecision
6.
Return Value of C++ setprecision
7.
Exceptions of C++ iomanip setprecision() function
7.1.
C++
8.
Significant Figures and Decimal Places
9.
Examples of C++ setprecision function
9.1.
Example 1: Using std::setprecision() function 
9.2.
C++
9.3.
Example 2: Default Precision and Maximum Precision
9.4.
C++
9.5.
Example 3: Using std::fixed to Set Precision in Decimal Places
9.6.
C++
9.7.
Example 4: Using std::fixed to find default precision and maximum precision
9.8.
C++
10.
Where and how can we use setprecision in C++?
11.
Frequently Asked Questions
11.1.
What does fixed and Setprecision do in C++?
11.2.
What is Setprecision in C++ 11?
11.3.
What is the difference between Setw and Setprecision in C++?
11.4.
Which library is Setprecision in C++?
11.5.
Does Setprecision affect integers?
11.6.
What is the default precision in C++?
11.7.
What is the difference between fixed and Setprecision in C++?
12.
Conclusion
Last Updated: Aug 13, 2024
Easy

set::precision in C++

Author Urwashi Priya
0 upvote

Introduction

In programming, decimal numbers are very tricky to handle, from storing them in memory to their representation. In C++, you can control the formatting of decimal numbers in the input/output streams along with the number of significant figures by utilizing methods and properties of the iomanip header file.

set::precision in C++

In this article, you will learn about the setprecision stream modifier along with some basics of stream formatting using the iomanip header file in C++ with the help of examples. Also see Literals in C.

What is Set precision in C++??

The setprecision() method is a built-in feature in C++ found within the <iomanip> header file. This method serves the purpose of modifying floating-point values by specifying the precision for numbers following the decimal point.

Syntax of C++ setprecision

setprecision(int n);


Here, the argument n decides the total number of a significant figure in the given integer. This parameter is of integer type, and it is a necessary parameter. 

How SetPrecision in C++ Works?

  • std::setprecision is a manipulator in C++ that affects the precision of floating-point output in streams.
  • It is part of the <iomanip> header in the C++ Standard Library.
  • When used with std::cout, it sets the number of digits to display after the decimal point for floating-point values.
  • It takes an integer argument specifying the desired precision.
  • The precision affects both fixed-point notation and scientific notation.
  • If precision is set to n, it guarantees that at least n digits are displayed in total, including digits before and after the decimal point.
  • The actual behavior may depend on the floating-point representation used by the system.

Parameters of C++ setprecision

The setprecision function expects a single integer parameter ‘n’ that specifies the desired precision.

  • If ‘n' is zero:
    The number is rounded off to the nearest integer.
     
  • If ‘n’ is -1:
    The default precision is used.
     
  • Otherwise,
    Depending upon the formatting, the number of digits before and after the decimal is restricted to n.

Return Value of C++ setprecision

setprecision doesn't return any values, it is only used for manipulating the input/output streams to modify the formatting of decimal numbers.

Also Read - C++ Interview Questions

Exceptions of C++ iomanip setprecision() function

The std::setprecision function behaves depending upon the formatting of the stream. It is important to use setprecision according to the formatting set using std::fixed, std::scientific, std::defaultfloat, std::showpoint, etc.

The following code in C++ illustrates the changing behavior of setprecision() function depending on the selected formatting.

  • C++

C++

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double num = 1013.4567;
cout << setprecision(2);
cout << showpoint << num << "\n";
cout << fixed << num << "\n";
cout << defaultfloat << num << "\n";
cout << scientific << num << "\n";
}
You can also try this code with Online C++ Compiler
Run Code


Output:-

1.0e+03
1013.46
1.0e+03
1.01e+03

Significant Figures and Decimal Places

Significant figures in a number carry information related to the precision of that value. They represent the number of reliable values in a measured value or result. All non-zero digits and zeros between non-zero digits are significant while leading zeros are not considered significant digits. 

For example:-

456 has 3 significant digits 
0.0003 has 1 significant digit
101 has 3 significant digits


Decimal places indicate the number of digits present in a number after the decimal.

For example:-

456.123 has 3 decimal places
2.00 has 2 decimal places

Examples of C++ setprecision function

Example 1: Using std::setprecision() function 

  • C++

C++

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double num = 1013.4567;
cout << setprecision(2) << num << "\n";
}
You can also try this code with Online C++ Compiler
Run Code


Output:-

1.0e+03


In this example, setprecision applies a significant figure limit of 2 on both before and after the decimal combined as the default formatting is used.

Example 2: Default Precision and Maximum Precision

  • C++

C++

#include <iostream>
#include <iomanip>
#include <limits>

using namespace std;

int main () {

double e = 2.718281828459045;

cout<< "Default precision: " << e << "\n";
cout<< "Max precision : " << setprecision(numeric_limits<double>::digits10 + 1) << e << "\n";
return 0;

}
You can also try this code with Online C++ Compiler
Run Code


Output:-

Default precision: 2.71828
Max precision : 2.718281828459045


The default precision is 6 which limits the total number of significant figures before and after the decimal.

Example 3: Using std::fixed to Set Precision in Decimal Places

  • C++

C++

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double num = 1013.4567;
cout << setprecision(2);
cout << fixed << num << "\n";
}
You can also try this code with Online C++ Compiler
Run Code


Output:-

1013.46


std::fixed changes formatting of the input/output stream to fixed from std::defaultfloat which is the default formatting in C++.

Example 4: Using std::fixed to find default precision and maximum precision

  • C++

C++

#include <iostream>
#include <iomanip>
#include <limits>

using namespace std;

int main () {

double e = 2.718281828459045;

cout << fixed;
cout<< "Default precision: " << e << "\n";
cout<< "Max precision : " << setprecision(numeric_limits<double>::digits10 + 1) << e << "\n";
return 0;

}
You can also try this code with Online C++ Compiler
Run Code


Output:-

Default precision: 2.718282
Max precision : 2.7182818284590451


In the above example, the default precision of 6 is applied to the digits after the decimal, without fixed formatting, only 5 digits after the decimal would have been printed.

Where and how can we use setprecision in C++?

To format our output in floating-point numbers, we use setprecision in C++. Here, if we use the “fixed” keyword before setprecision then the parameter decides only the count after the decimal point else it considers the significant figure. 

Note: setprecision in C++ gives a round-off value.

Also, check out this article - Pair in C++

Try and compile with online c++ compiler.

Frequently Asked Questions

What does fixed and Setprecision do in C++?

In C++, `fixed` and `setprecision` format output. `fixed` ensures that floating-point numbers are displayed in fixed-point notation, while `setprecision` sets the number of decimal places.

What is Setprecision in C++ 11?

`setprecision` in C++ 11 is a function from the `<iomanip>` library that allows you to set the precision (number of decimal places) for floating-point output.

What is the difference between Setw and Setprecision in C++?

`setw` is used to set the field width for output formatting in C++, while `setprecision` is used to set the precision (decimal places) for floating-point numbers.

Which library is Setprecision in C++?

`setprecision` is a function provided by the `<iomanip>` library in C++ to control the precision (number of decimal places) for floating-point output.

Does Setprecision affect integers?

No, `setprecision` in C++ specifically affects the formatting of floating-point numbers and does not impact the formatting of integers.

What is the default precision in C++?

The default precision in C++ is 6 significant digits.

What is the difference between fixed and Setprecision in C++?

std::fixed and std::setprecision are both stream manipulators which are part of the iomanip header file in GCC. std::fixed changes the formatting of the I/O stream to fixed. Meanwhile, std::setprecision is used for setting the number of digits to be displayed before and after the decimal depending on the formatting. 

Conclusion

In this article, we have discussed the details of the method set::precision in C++ with examples.

We hope that the blog has helped you enhance your knowledge regarding the set::precision in C++. You can refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. To practice and improve yourself in the interview, you can also check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews. Do upvote our blogs to help other ninjas grow. 

Live masterclass