1

I was about to ask this question, and found a few more to ask.

How (according to the top answer) would I then proceed to only display this precision for a single call to cout, and then disable it thereafter?

Say I want to show precision for the first three calls, but not the last:

(I named a variable with the same name as the "fixed" format specifier in order to experiment)

#include <iostream>
int main(){
   using namespace std;
   int spam = 5;
   double flak = 5.0;
   double result = spam * flak;
   double fixed = 42;
   cout.precision(1);
   cout << std::fixed << spam + flak << endl;
   cout << result << endl;
   cout << flak << endl;
   cout << fixed;
   return 0;
}
Community
  • 1
  • 1
Leonardo
  • 1,452
  • 3
  • 15
  • 26

3 Answers3

4

You can invoke the precision function again to re-apply it like so:

int main(){
   using namespace std;
   int spam = 5;
   double flak = 5.0;
   double result = spam * flak;
   double fixed = 42;
   cout.precision(1);
   cout << std::fixed << spam + flak << endl;
   cout << result << endl;
   cout << flak << endl;
   cout.precision(3);
   cout << fixed;
   return 0;
}

Another way to do it is to use the <iomanip> header to use the std::setprecision() function and passing it to std::cout, so it would be similar to doing this:

#include <iomanip>
#include <iostream>

int main() {
   int spam = 5;
   double flak = 5.0;
   double result = spam * flak;
   double fixed = 42;
   std::cout << std::fixed << spam + flak << std::endl;
   std::cout << std::setprecision(1) << result << '\n' << flak << std::endl;
   std::cout << std::setprecision(0) << fixed;
   return 0;
}
Rapptz
  • 20,807
  • 5
  • 72
  • 86
3

Not sure what you want. But C++ provides some ways to control the state of stream.

std::streamsize prec = cout.precision(); // store current precision setting
// some print here
std::cout.precision(prec)                // Roll-back
halfelf
  • 9,737
  • 13
  • 54
  • 63
  • I wish there was a "scoped precision" class which restored it with RAII. – user541686 Dec 27 '12 at 05:41
  • I was not aware that calling cout.precision() with no parameter returned a number. I tried 'cout << cout.precision();' and it did not output anything. – Leonardo Dec 27 '12 at 05:44
  • RAII wont help in this case because the changes need to happen within a function, unless you want to scope all such changes with braces – Karthik T Dec 27 '12 at 05:45
  • @Leonardo It does output on my machine. The default precision is 6.(OS X, clang++, libc++) – halfelf Dec 27 '12 at 05:47
1

One (ugly) way would be to cast it to int before the cout if you dont want to see the decimal places.

Something like (C style - still ok for primitive types)

cout << (int)flak << endl;

or ( C++ style - recommended, especially for non primitive types like classes)

cout << static_cast<int>(flak) << endl;

This will only change what cout sees, not the variable itself.

Karthik T
  • 31,456
  • 5
  • 68
  • 87
  • Is that very wise to change the type of variable in order to use it? And would it no longer be a double if I used it elsewhere in the code? – Leonardo Dec 27 '12 at 05:34
  • @Leonardo the type itself would still be a `double`, and the C++ way of doing a cast like that would be a `static_cast()` where T is the type you want to cast it to. You can read [here](http://stackoverflow.com/questions/28002/regular-cast-vs-static-cast-vs-dynamic-cast) for more info on casts. – Rapptz Dec 27 '12 at 05:35
  • Yes I see that this does indeed work, and luckily the loss of precision will only affect the result of the call to that cout. – Leonardo Dec 27 '12 at 05:41
  • "C style - still ok for primitive types)" - what makes you say that? It ignores (and silently removes) `const`-ness and it doesn't express intent very well (you have to read the surrounding code to see why it was there). On top of that it is impossible to grep/search for it effectively in a large code base. – utnapistim Dec 27 '12 at 10:03