Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printf float with number of trailing zeros as variable [duplicate]

printf("%.2f",..);

I want to control the number 2 in the example above, put a variable instead of the the number 2.

so if

int var=5;

the argument of printf will be "%.5f".

Is it possible? Thanks.

like image 691
Signer3 Avatar asked Mar 26 '26 07:03

Signer3


1 Answers

This should work for you:

A little example program to test it:

#include <stdio.h>

int main() {

    float f = 4.3234;
    int x = 2;

    printf("%.*f", x, f);

    return 0;

}

For more information see: http://www.cplusplus.com/reference/cstdio/printf/

like image 175
Rizier123 Avatar answered Mar 27 '26 21:03

Rizier123