+ 1
how to print float 3.14 as 3.140 in c++ means 3 digit after decimal point
2 Antworten
+ 6
the answer is here:
http://www.cplusplus.com/reference/iomanip/setprecision/
// 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;
}
+ 2
if you're using cout you can do something like;
float x = 3.14;
cout << setiosflags (ios::fixed);
cout << setprecision (3);
cout << x;
you'll need # include <iomanip> in order to get this to work.
The fixed indicates a fixed number of decimal places and the 3 in the setprecision is how many