Floating Conversion Integer in C

float f1 = 123.125;
int i1 = -150;

f1 = i1;    // integer to floating conversion
printf("%i assigned to an float produces %f\n", i1, f1);

Conclusion:

-150 assigned to an float produces -150.000000

My question is: why after the .result has 6 zeros ( 000000), and not 7 or 8 or some number?

+3
source share
4 answers

This is what it does printf. Refer. Reference page , which says

f, f

"[-] ddd.ddd", radix . , 6; , '#' , . , . .

( )

, 150 ( double, printf - varargs).

+7

, , , %f printf. .

+3

C (ยง7.19.6.1) , , %f 6 .

f,F , , [-] ddd.ddd, . , 6; , # , .

+3

. printf .

:

By default, the number of digits after the decimal point is six, but this can be changed using the precision field. If a decimal point appears, at least one digit appears in front of it. The "double" value is rounded to the desired number of decimal places.

+2
source

Source: https://habr.com/ru/post/1788754/


All Articles