floating rounding during output

I and trying to have a float display a certian number past decimal point no matter if the number is 1.0 or 1.1 of 1.11 or 1.1111. I can't find anything that can help me in the cout statement. It can be 1 ,2 , 3 numbers past decimal point just need help on how to format output

for example:
e=(2*9)/2.1
the answer to this is 8.571428571
I can't figure out how to format the output so is displays up to 3 decimal places or maybe 2 or maybe one decimal place but i don't need he long answer.
example of the output i am looking for is 8.57

Any help from anyone is greatly appreciated
Try with something like this:
1
2
cout.precision(3);
cout << fixed << Whatever_You_Want_To_Display;
This will shoe 3 decimal digits
Last edited on
it helped but when it prints on screen i get 1 place after decimal point i need to have at least two show in some cases. it seems the out put is rounded up even if i don't want it to any help with that problem.

Thank you Bazzy for the help and any more you can give me.
for n digits enter this line towards beginning, keep in mind you can change it up throughout your program by adding the line where ever you need it re-adjusted.

cout << fixed << showpoint << setprecision(n);

n = how many decimal placed you want to show.

for example:
cout << fixed << showpoint << setprecision(8);
double n = 1/3;
cout << n << endl;
cout << fixed << showpoint << setprecision(7);
cout << n << endl;
cout << fixed << showpoint << setprecision(3);
cout << n << endl;
cout << fixed << showpoint << setprecision(6);
cout << n << endl;
cout << fixed << showpoint << setprecision(2);
cout << n << endl;

it would display
0.33333333
0.3333333
0.333
0.333333
0.33
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

using namespace std;
 float getera(int runs, float ip);
int main()
{

 int runs,end;
 float era, ip;
 end=9;
 do
 {
       cout << "Enter the numbers of runs allowed ";
       cin >> runs;
       cout << "\n";
       cout << "Enter the number of innings pitched ";
       cin >> ip;
       cout << "\n";
       cout << " The earned run average = "; 
       era=getera(runs,ip);
       cout << era << setprecision(2);
       cout << "\n";
       cout << " Enter 0 to quit or anything else to continue ";
       cout <<"\n";
       cin >> end;
   }while (end > 0);   
 return 0;   
}    

float getera(int rs, float i)
{
       float er;
       er=(rs*9)/i;
 return er;
}     

get setprecision undeclared error do i need another library file to include. Useing DevC++ as ide and compiler
Found my answer. Thank you for all your help. Now all i have to do is figure out how i get one answer using the formula and the place where i got the formula come up with different results but that is not your problem. Again thank you for your help
Topic archived. No new replies allowed.