Printf width specifier to maintain precision of floating-point value

后端 未结 8 1390
萌比男神i
萌比男神i 2020-11-21 13:39

Is there a printf width specifier which can be applied to a floating point specifier that would automatically format the output to the necessary number of s

8条回答
  •  無奈伤痛
    2020-11-21 14:09

    Simply use the macros from and the variable-width conversion specifier (".*"):

    float f = 3.14159265358979323846;
    printf("%.*f\n", FLT_DIG, f);
    

提交回复
热议问题