I declare a variable double MyDouble;
.
If I then set MyDouble = 1000.0 / 3.0;
and then
printf("%g", MyDouble);
gives 333.333 so it's lost some precision.
If I use printf("%lf", MyDouble);
I get the precision.
The down side is if I now set MyDouble = 5.0 / 2.0;
and use printf("%lf", MyDouble);
I get 2.500000, so trailing zeroes.
As a general case, how can I have the precision without the trailing zeroes. I could write the double to a string using snprintf
and the %lf
format and then write a subroutine to strip trailing zeroes, but there must be an easier way.