Reputation: 41
double a = 2451550;
double b = .407864;
double c= a*b;
cout<<c;
I was expecting the results to be "999898.9892" but getting "999899". I need the actual unrounded result.Please suggest.
Upvotes: 4
Views: 4474
Reputation: 5431
By default the precision of an std::iostream will show how many digits total to display and by default precision is 6. So, since your number has six digits before the decimal place it will not display any after the decimal.
You can change this behavior with the 'fixed' manipulator. 'precision' then changes to mean the number of digits to display after the decimal which is probably what you were expecting.
To always get four digits after the decimal you can do this:
cout << setprecision(4) << fixed << c;
However, keep in mind that this will always display four digits after the decimal even if they are zeros. There is no simple way to get 'precision' to mean at most x number of digits after the decimal place with std::iostreams.
Upvotes: 0
Reputation: 6342
It can also be done using Manipulator setprecision like below.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double a = 2451550;
double b = .407864;
double c= a*b;
cout<<setprecision(15)<<c;
}
Also, Usage of manipulator will make the code compact.
Upvotes: 1
Reputation: 29055
By default, iostreams output 6 digits of precision. If you want more, you have to ask for it:
std::cout.precision(15);
Upvotes: 14