I have a list of float
values and I want to print them with cout
with 2 decimal places.
For example:
10.900 should be printed as 10.90
1.000 should be printed as 1.00
122.345 should be printed as 122.34
How can I do this?
( setprecision
doesn't seem to help in this.)
You have to set the 'float mode' to fixed.
this an example using a matrix.
To set fixed 2 digits after the decimal point use these first:
Then print your double values.
This is an example:
With
<iomanip>
, you can usestd::fixed
andstd::setprecision
Here is an example
And you will get output
setprecision(n)
applies to the entire number, not the fractional part. You need to use the fixed-point format to make it apply to the fractional part:setiosflags(ios::fixed)
I had an issue for integers while wanting consistent formatting.
A rewrite for completeness: