Dynamic Float Format Specifier in C

前端 未结 2 1788
陌清茗
陌清茗 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条回答
  • 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);
    
    0 讨论(0)
  • 2020-12-18 23:51

    printf("%.*f", n, c); that will print out c with n places after the decimal.

    0 讨论(0)
提交回复
热议问题