ChatGPT解决这个技术问题 Extra ChatGPT

C: printf a float value

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.
If you are using 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
@DavidHeffernan How many meaningful digits are there?
Now is an appropriate time to bring up "Floating Point Numbers Aren't Real": freshsources.com/FPNotReal.pdf
@barlop Six years later, I think I'll live with it man

S
S.S. Anne

You can do it like this:

printf("%.6f", myFloat);

6 represents the number of digits after the decimal separator.


Thanks! I though of it, but I just thought It couldn't be so easy!
The 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.
S
Sergey Kalinichenko

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.
@dasblinkenlight with 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 ?
And one of the website codingunit.com/… explaining ` %3.2f ` as (print as a floating point at least 3 wide and a precision of 2) . So I am confuse now
@xyz In C's 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.
J
J0e3gan
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.


T
Tarion
printf("%.<number>f", myFloat) //where <number> - digit after comma

http://www.cplusplus.com/reference/clibrary/cstdio/printf/


be carefull! the f specifier have to be after the number of digits so printf("%.numberf", myFloat) Tank you for the useful link!
R
Roberto A. Foglietta

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

P
Pang

Use %.6f. This will print 6 decimals.


S
Shankar Raju

You need to use %2.6f instead of %f in your printf statement


It isn't correct. Format "%2.6f" means total width of 2 characters with 6 characters after decimal point.
@ValeriyVan This is a bit like nitpicking, but I think it is important: The 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