user164054 user164054 - 1 year ago 109
C Question

'sprintf': double precision in C


double a = 0.0000005l;
char aa[50];
sprintf(aa, "%lf", a);
printf("%s", aa);

Output: s0.000000

In the above code snippet, the variable
can contain only six decimal precision. I would like to get an output like "s0.0000005". How do I achieve this?

Answer Source

From your question it seems like you are using C99, as you have used %lf for double.

To achieve the desired output replace:

sprintf(aa, "%lf", a);


sprintf(aa, "%0.7f", a);

The general syntax "%A.B" means to use B digits after decimal point. The meaning of the A is more complicated, but can be read about here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download