How do i get a number to display only two decimal points? I'm using a float variable, and i know that it's default is 6 decimal spots. Is there a way to change this? or a new variable type?
Printable View
How do i get a number to display only two decimal points? I'm using a float variable, and i know that it's default is 6 decimal spots. Is there a way to change this? or a new variable type?
Code:float f = 0.333;
cout.setprecision( 2 );
cout<<f<<endl; //Should output 0.33
i get the following error:
i've inly included the following:Quote:
error C2039: 'setprecision' : is not a member of 'ostream_withassign'
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
aha, i got it, its "precision" w/o the "set". thanks.
Try this...
... this should work. setprecision() is the ANSI standard manipulator. I suspect it failed in your setup because you are using the old style headers, (those with .h on them), and not the standard compliant ones declared in namespace std.Code:#include <iostream>
#include <iomanip>
using namespace std;
...
...
cout << setprecision(2);
If you try this and it fails, tell me what compiler you are using.
>>#include <iomanip>
This was the missing item causing the problem. It's where setprecision lives.