+ 1

how to print float 3.14 as 3.140 in c++ means 3 digit after decimal point

28th Apr 2017, 7:51 PM
Gadget tester
Gadget tester - avatar
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; }
28th Apr 2017, 8:09 PM
Burey
Burey - avatar
+ 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
28th Apr 2017, 8:09 PM
Shane