setprecision c++ | How to Use setprecision in c++

Set decimal precision. Sets the decimal precision to be used by output operations. Behaves as if a call to the stream's member ios_base::precision with n. In C++, you can use the 'precision' function to set the precision of the output. The function 'precision' is present in the header file 'iostream.h'. This describes the C++ Standard Template Library (STL) setprecision function for output stream manipulation.

0
function (setprecision c++ | How to Use setprecision in c++)

If you are new in C++ , and want to output point number up to 2 digits in C++. For example, if number is 3.444, then the output should be 3.44 or if number is 88999.4234 then output should be 88999.42, How can i do that. Here is my code. setprecision c++ | How to Use setprecision in c++

#include <iomanip.h>
#include <iomanip>
int main()
{
double num1 = 3.12345678;
cout << fixed << showpoint;
cout << setprecision(2);
cout << num1 << endl;
}

std::setprecision

/*unspecified*/ setprecision (int n);

Set decimal precision
Sets the decimal precision to be used to format floating-point values on output operations.Behaves as if member precision were called with n as argument on the stream on which it is inserted/extracted as a manipulator (it can be inserted/extracted on input streams or output streams).This manipulator is declared in header <iomanip>.

Parameters (setprecision c++ | How to Use setprecision in c++)

n
New value for the decimal precision.

Return Value

Unspecified. This function should only be used as a stream manipulator (see example).

Example for setprecision c++ | How to Use setprecision in c++

// setprecision example
#include <iostream>     // std::cout, std::fixed
#include <iomanip>      // std::setprecision

int main () {
double f =3.14159;
std::cout << std::setprecision(5) << f << '\n';
std::cout << std::setprecision(9) << f << '\n';
std::cout << std::fixed;
std::cout << std::setprecision(5) << f << '\n';
std::cout << std::setprecision(9) << f << '\n';
return 0;
}
Output:

3.1416 3.14159 3.14159 3.141590000

You can also see these:

Best laptop for programming | Good programming laptop 2017

Programming Languages that you must learn in 2017

Programming languages that are in demand

Most popular programming languages

Best Laptop for Programming and Developers 2017

Best linux laptop for developers 2017