Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Float to int if the num has no numbers behind decimal point in C [closed]

Tags:

c

how am I supposed to print a float so as if it has no numbers behind decimal point e.g. 11.00 should be printed as 11 but 11.45 should stay the same. The problem is some if statement maybe. Any suggestions?

like image 391
MartinT Avatar asked Oct 30 '22 07:10

MartinT


1 Answers

First solution that comes on my mind is cast. This is what I would do. Let's say your variable is "a", that you want to print.

    float a;

    if (if (a-(int)a<0.001 || (int)a-a<0.001) ) //1st comment explains this 
       printf("%d", (int)a);
    else
       printf("%f", a);
like image 185
S.Mitric Avatar answered Nov 13 '22 22:11

S.Mitric