float aver; int sum, n; aver=sum/n; We get the omit one. float aver; int sum, n; aver=(float)sum/n; //(In fact , it is ' (float)(sum) / n; ') We get the round one. Assign value directly -> omit int a; float b; b=3.51; a=b; print a and we get 3 And '(int)float_number' also get the omitted one !!! But use '%.f' we get the round one!!! According to the C-FAQ 14.6, the simplest and most straightforward way to round number is to use this: ' (int)(x+0.5); ' However, it can be used to round number for positive numbers but not for negative ones. So a better one is: ' (int)(x<0? x-0.5 : x+0.5) ' To round to a certain precision by scaling: ' (int)(x / precision + 0.5)*precision

评论