2

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;
gen
  • 9,528
  • 14
  • 35
  • 64

2 Answers2

8

Using stream manipulators fixed and setprecision:

#include <iomanip>

float f = 2.3333;
std::cout << std::setprecision(2) << std::fixed << f;
jrok
  • 54,456
  • 9
  • 109
  • 141
1

I managed to solve it without iomanip:

cout << (((int)f*100) % 100)/100; 
gen
  • 9,528
  • 14
  • 35
  • 64