I know this question has been asked before but in a bunch of different contexts which I could not really understand(as I am new to the c++ language,previously from java). My question related to std::fixed
and setprecision()
in iomanip
.
I have the following simple double:
double price = 7.50001;
With this I wanted it to output as per a normal price as so:
cout<< "The price is:$" <<fixed<<setprecision(2)<< price <<endl;
However now due to std::fixed
every cout
I use from now on will have precision(2)
. My question is how do I roll back to the previous precision that is if I add another double say 6.5100000
the output will be 6.5
, a double such as 6.200000
will be 6.2
, 6.15555
will be 6.15555
etc. (as per before).
Note I have tried using this as per google:
std::streamsize ss = std::cout.precision();
... And then resetting the precision using ss, however this does not work as the output still includes trailing zeros which it did not before. For example 6.50
outputs 6.50
when before it output 6.5
(this is true for any double previously,it eliminated trailing zeros).