'sprintf': double precision in C

后端 未结 3 888
南笙
南笙 2021-01-04 01:22

Consider:

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

Output: s0.000000

In the above code snippet, th

3条回答
  •  执念已碎
    2021-01-04 01:57

    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);
    

    with

    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.

提交回复
热议问题