0

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 =((

4
Contributors
12
Replies
20
Views
6 Years
Discussion Span
Last Post by sha11e
0

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

0

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? >_>

0

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.

0

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

0

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.

0

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.

0

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

Edited by WaltP: n/a

0

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); .

Edited by mike_2000_17: n/a

0

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

0

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
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.