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.
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.
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/
Check this
int main(int argc, char* argv[])
{
char format[16];
int number;
number = 5;
snprintf(format, sizeof(format), "%%.%df", number);
printf("%s\n", format);
return 0;
}