When writing this:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
float number = 2.12345;
cout <<fixed <<setprecision(3) <<number;

return 0;
}

I get 2.123
If I then cout another float variable that I want all decimal numbers at, and not setprecision(3)...... How do I do that?
If I do something like setprecision(50) to get all decimals I will get more than the number is, for example 2.123 -> 2.123000000000000000000 etc.

Help =((

Recommended Answers

All 12 Replies

Don't use setprecision if you want the computer to figure out the precision.

i am not sure but i think you willhave 3 decimals points until you put again setprecicion.

if you put later seprecision(5) will be from that line and down with 5,

i repeat i am not sure,just that sounds logical to me :P


and about the setprecision(50) try it :D put and compile to see what you will have :D

Don't use setprecision if you want the computer to figure out the precision.

But maybe I want precision on one number but not the rest? >_>

setprecision only affects the next write operation. So you can setprecision on the numbers you want to have a fixed precision on and don't use it for those that you don't want precision on.

setprecision only affects the next write operation. So you can setprecision on the numbers you want to have a fixed precision on and don't use it for those that you don't want precision on.

This is the code:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
float number = 2.12345;
cout <<"Without setprecision: "<<number<<endl;
cout <<"With fixed and setprecision(3): " <<fixed <<setprecision(3) <<number <<endl;
cout <<"The variable number again: " <<number;


cin.get(); cin.get();
return 0;
}

That will output:

Wihtout setprecision: 2.12345
With fixed and setprecision(3): 2.123
The variable number again: 2.123

So did you try

cout <<"Without setprecision: "<<number<<endl;
cout <<"With fixed and setprecision(3): " <<fixed <<setprecision(3) <<number <<endl;
cout <<"The variable number again: " <<setprecision(0) <<number;

That would be my logical test next.

So did you try

cout <<"Without setprecision: "<<number<<endl;
cout <<"With fixed and setprecision(3): " <<fixed <<setprecision(3) <<number <<endl;
cout <<"The variable number again: " <<setprecision(0) <<number;

That would be my logical test next.

Then it just outputs 2.

setprecision only affects the next write operation. So you can setprecision on the numbers you want to have a fixed precision on and don't use it for those that you don't want precision on.

Actually, setprecision() affects the stream, not just the next output. See this

Actually, setprecision() affects the stream, not just the next output. See this

Yeah, my bad. I confused it with setw(). That's what I get for being a smart*ss.


I think, however, that since setprecision will only give an upper-bound. If you don't use "fixed", then resetting precision to a large number (like 15), it should bring it back to a somewhat default behaviour.

NB: You can unset the "fixed" format by calling cout.unsetf(ios_base::floatfield); .

Yeah, my bad. I confused it with setw(). That's what I get for being a smart*ss.


I think, however, that since setprecision will only give an upper-bound. If you don't use "fixed", then resetting precision to a large number (like 15), it should bring it back to a somewhat default behaviour.

NB: You can unset the "fixed" format by calling cout.unsetf(ios_base::floatfield); .

didn't work.
Try it yourself, doesnt seem to work =(

Grah, I would think something like this owuld be easy

The solution I posted does work. Here is the code:

#include <iostream>
#include <iomanip>

using namespace std;

int main() {

  double d = 1.23456;

  cout << "Default format: " << d << endl;
  cout << "Fixed format: " << fixed << d << endl;
  cout << "Fixed prec3 format: " << setprecision(3) << d << endl;
  cout.unsetf(ios_base::floatfield);
  cout << "Default format: " << setprecision(10) << d << endl;

  return 0;
};

//OUTPUT:
// Default format: 1.23456
// Fixed format: 1.234560
// Fixed prec3 format: 1.235
// Default format: 1.23456

The solution I posted does work. Here is the code:

#include <iostream>
#include <iomanip>

using namespace std;

int main() {

  double d = 1.23456;

  cout << "Default format: " << d << endl;
  cout << "Fixed format: " << fixed << d << endl;
  cout << "Fixed prec3 format: " << setprecision(3) << d << endl;
  cout.unsetf(ios_base::floatfield);
  cout << "Default format: " << setprecision(10) << d << endl;

  return 0;
};

//OUTPUT:
// Default format: 1.23456
// Fixed format: 1.234560
// Fixed prec3 format: 1.235
// Default format: 1.23456

I suppose.
Doesn't work with float though, even if I try to cout a float after that code.
And why do you do setprecision(10)? i tried 99 but then it did like 1.2345600000000001

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.