I want to globally set the ouptut precision to 2 decimal places.
I already tried to use iomanip
and setprecision
, however I keep getting output with 'e' in it.
This is my example code:
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
double pay=16.78;
double hours;
double total;
cout.precision(2);
cout << "Please enter how many hours you worked : " << endl;
cin >> hours;
total=hours*pay;
cout << "You earned: " << total << endl;
}
You can use something like this:
You will need to include iomanip for this to work.
If your e is a positive value, you cannot ride of them because your value is too large. This code
If it's a negative number, your precision is not enough. Like the following code
A simple way will be to use std::fixed
But the disavantage you have with the std::fixed is it shows the zero after the last none-zero number until it reach the setprecision qunatity you set previously.
I'm not familiar with "cout.precision(2)". I would use std::setprecision(4) if I wanted my output to have 4 significant figures.