Dynamic Float Format Specifier in C

前端 未结 2 1789
陌清茗
陌清茗 2020-12-18 23:25

Is there any way to have a user inputed float format specifier? For example, if I print this.

float c = 15.0123
printf(\"%.2f\", c);

// outputs: 15.01
         


        
2条回答
  •  梦毁少年i
    2020-12-18 23:41

    The precision can be specified by an argument with the asterisk *. This is called an argument-supplied precision.

    float c = 15.0123;
    int m = 2;
    printf("%.*f", m,  c);
    

提交回复
热议问题