Question

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 ?

No correct solution

OTHER TIPS

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top