Reputation: 99
I have this code:
double a = 7.456789;
cout.unsetf(ios::floatfield);
cout.precision(5);
cout << a;
and also this one:
double a = 798456.6;
cout.unsetf(ios::floatfield);
cout.precision(5);
cout << a;
the result of the first code is: 7.4568 Which is almost what I want (what I want to recieve is 7.4567) the result of the second : 7.9846e+05 Which is not at all what I want (I want 798456.6) I want to print the number till 4 numbers after the decimal point
How can I do that ?
Upvotes: 0
Views: 1790
Reputation: 596307
By using unsetf()
, you are telling cout
to use its default formatting for floating-point values. Since you want an exact number of digits after the decimal, you should be using setf(fixed)
or std::fixed
instead, eg:
double a = ...;
std::cout.setf(std::fixed, ios::floatfield);
std::cout.precision(5);
std::cout << a;
.
double a = ...;
std::cout.precision(5);
std::cout << std::fixed << a;
Upvotes: 4