# Thread: Printing float number with incremented precision

1. ## Printing float number with incremented precision

Hi

The following code gives the corresponding print:
Code:
```floatValue = 12345.67890;

printf("%.1f\n", floatValue);
printf("%.2f\n", floatValue);
printf("%.3f\n", floatValue);
printf("%.4f\n", floatValue);
printf("%.5f\n", floatValue);```
Print:
12345.7
12345.68
12345.679
12345.6789
12345.67890

Is there a way to make this precision increment in a loop instead?
i.e.
Code:
```for(int i=1; i < 6; i++){
printf("%.if\n", floatValue);
i++;
}```
BR,
Flanderso

2. It's actually in the manual if you look.

printf("%.*f\n", i, floatValue);

3. Thanks a lot whiteflags!

Btw, what manual?

4. Ages ago, you used to place the cursor on say printf and press the F1 key to get information on that function.

Nowadays, you can typically use a search engine with the word 'man' and the function of interest.

For it to output that, the variable floatValue must actually be declared as a double, not a float.

6. Originally Posted by iMalc
For it to output that, the variable floatValue must actually be declared as a double, not a float.
What? I thought for sure that a float is promoted to double when passed to a variadic function (or when there is no function declaration) like printf.

7. Originally Posted by christop
What? I thought for sure that a float is promoted to double when passed to a variadic function (or when there is no function declaration) like printf.
I think what iMalc means is that, usually, float values do not have the kind of precision indicated by the example.
For an 32-bit IEEE floating point number, with 23+1 binary digits of precision, the precision of the decimal expansion (~7 digits) is not enough for the result shown.

8. Originally Posted by qny
I think what iMalc means is that, usually, float values do not have the kind of precision indicated by the example.
For an 32-bit IEEE floating point number, with 23+1 binary digits of precision, the precision of the decimal expansion (~7 digits) is not enough for the result shown.
Yes exactly.
It might be promoted to double for the printf call, something that was not of interest to me at the time. If the values were stored in floats then the promoted value would still only have about 6 significant decimal digits in the output due to the time that the value spent being held just inside the float variable. Therefore the variable is definitely a double.