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);
<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;
}
3.1416 3.14159 3.14159 3.141590000
You can also see these:
- ios_base::precision
- Get/Set floating-point decimal precision (public member function )
- fixed
- Use fixed floating-point notation (function )
- scientific
- Use scientific floating-point notation (function )
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
Leave a Reply