I want to print a float value which has 2 integer digits and 6 decimal digits after the comma. If I just use printf("%f", myFloat)
I'm getting a truncated value.
I don't know if this always happens in C, or it's just because I'm using C for microcontrollers (CCS to be exact), but at the reference it tells that %f
get just that: a truncated float.
If my float is 44.556677
, I'm printing out "44.55"
, only the first two decimal digits.
So the question is... how can I print my 6 digits (and just the six of them, just in case I'm having zeros after that or something)?
%2.6f
sounds like the correct format string, if your runtime library supports it.
float
then you won't get 8 meaningful significant decimal digits in any case. You are asking for more precision than the data type posesses. The nearest float to 44.556677
is 44.55667 87719 72656 25
You can do it like this:
printf("%.6f", myFloat);
6 represents the number of digits after the decimal separator.
printf("%9.6f", myFloat)
specifies a format with 9 total characters: 2 digits before the dot, the dot itself, and six digits after the dot.
%09.6f
might also be an option.
printf("%9.6f", myFloat)
, if I give 3 digit before dot then it still print the 3 digits before dot. can you please explain why so ?
printf
a %N
, where N
is a number, always defines the overall minimum width of the complete output of a field. %6s
always prints 6 characters, at least (more if the string is longer). Shorter outputs will be filled (in that case at the left side, use -N
to fill on the right). There is no exception for floats. %3f
prints at least 3 characters, so 1.0
is output as __1
(two leading spaces shown as _
) where 1.1
is output as 1.1
with no filling, it already has 3 characters. Hence in %N.Mf
there are, at least, N-M-1
characters of room before the dot.
printf("%0k.yf" float_variable_name)
Here k
is the total number of characters you want to get printed. k = x + 1 + y
(+ 1
for the dot) and float_variable_name
is the float variable that you want to get printed.
Suppose you want to print x digits before the decimal point and y digits after it. Now, if the number of digits before float_variable_name is less than x, then it will automatically prepend that many zeroes before it.
printf("%.<number>f", myFloat) //where <number> - digit after comma
http://www.cplusplus.com/reference/clibrary/cstdio/printf/
printf("%.numberf", myFloat)
Tank you for the useful link!
Try these to clarify the issue of right alignment in float point printing
printf(" 4|%4.1lf\n", 8.9);
printf("04|%04.1lf\n", 8.9);
the output is
4| 8.9
04|08.9
Use %.6f
. This will print 6 decimals.
You need to use %2.6f
instead of %f
in your printf statement
2
is not wrong here, but it is highly superfluous (not even redundant) which usually tells us, that the writer is confused on how printf works. %2.6f
means "at least 2 characters wide with 6 digits after the dot". As we will always see at least 8 characters, the 2 is far too low to have any effect here and it needs to be 9
or above to create some effect. Note that we do not talk Math here: In %+9.6f
the 9
still is not needed, as %+.6f
always has a width of at least 9: sign, digit, dot, 6 digits
Success story sharing
2
does not mean the number of digits before the decimal point, it means the minimum total field width (which will already be greater than 2), so here it does nothing.