<iomanip> and setprecision

Im tryna get my code (as seen bellow) to print both the variable "jax" with two decimal places, and the full value opf the variable "jax"

so the out out should be

8712.65

8712.654


How do i do this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <iomanip>
using namespace std;

int main()

{
    
double jax=8712.654;

  //first output
 cout<<setprecision(2)<<fixed;   
 cout<<jax<<endl<<endl;

 //second output
 cout<<jax<<endl<<endl;

 system("pause");   
}
Actually, printf is probably better. Both of these are c functions that need the <cstdio> header.

http://www.cplusplus.com/reference/clibrary/cstdio/printf/
You are obviously learning c++ so stick with the c++ headers please. First the two outputs are exactly the same because you didn't change anything between the two attempts to output jax.

http://cplusplus.com/reference/iostream/manipulators/setprecision/
In both the fixed and scientific notations, the precision field specifies exactly how many digits to display after the decimal point, even if this includes trailing decimal zeros. The number of digits before the decimal point does not matter in this case.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <iomanip>
using namespace std;

int main()

{

    double jax=8712.654;


    //first output
    cout<< setprecision(2) << fixed;   
    cout<< jax<< endl << endl;

    //second output
    cout << setprecision(3) << jax<< endl << endl;

    system("pause");   
}
Topic archived. No new replies allowed.