Truncate float so as to have only two decimals
16,892
Solution 1
Using stream manipulators fixed
and setprecision
:
#include <iomanip>
float f = 2.3333;
std::cout << std::setprecision(2) << std::fixed << f;
Solution 2
I managed to solve it without iomanip:
cout << (((int)f*100) % 100)/100;
Author by
gen
Updated on June 05, 2022Comments
-
gen almost 2 years
C++
I would like to
cout
float f = 2.3333
, but only with two decimals. How do I do that? I remember something like this, but it doesn't work:cout << f:2 << endl;