Given a variable of float type, how to output it with 3 digits after the decimal point, using iostream in C++?
Use setf and precision.
#include using namespace std; int main () { double f = 3.14159; cout.setf(ios::fixed,ios::floatfield); cout.precision(3); cout << f << endl; return 0; }
This prints 3.142
3.142