Printing a number to a given number of digits after decimal without rounding off in C++....

Printing a number to a given number of digits after decimal without rounding off in C++....

Went through tough time, while googling to find a trick to print a number to a given number of digits after decimal without rounding off in C++.

Here is the problem :-

Output needed : 1.66666

But with conventional way i.e. to use set precision function was getting output as 1.66667.

float a = 1.666666666; cout<<setprecision(6)<<a;

Output : 1.66667

To solve the above issue, I used the following way :-

float a = 1.666666666; float b = floor(a*1000000)/1000000; cout<<b;

Finally, got the required output i.e. 1.66666

Hope this helps to fellow coder out their......

Please, share if you have any other way to accomplish this..