2

The significant digits is 2.

Why the output of

cout << setprecision(2) << 0.999 << endl;` 

is 1 instead of 1.0?

1 Answers1

5

The default formatting does not print trailing zeros; you need to set the floating point formatting to fixed, see also this reference. So what you need is

cout << setprecision(2) << fixed << 0.999 << endl;

Note also that setprecision refers to the decimal digits, so for 1.0 you would need setprecision(1)

Karl Nicoll
  • 16,090
  • 3
  • 51
  • 65
dennis
  • 192
  • 7