I have a data type of double
, I want to printf
the double in the following form:
- If
double x=2
orx=2.0
print2
to the screen, which means if I get an integer I print an integer. - If
double x=2.3
orx=2.30
or any other non-whole number I print2.3
to the screen.
Is there any way to do it shortly? or do I need to use if statements or something like that?
double x = 2;
printf("%d", x);
double x = 2.3;
printf("%.1f", x);
This is an illustration only.
I want to printf
an integer or a double based on the value I get.