115

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)?

mskfisher
  • 3,291
  • 4
  • 35
  • 48
Roman Rdgz
  • 12,836
  • 41
  • 131
  • 207
  • 6
    `%2.6f` sounds like the correct format string, if your runtime library supports it. – Jon Dec 01 '11 at 17:29
  • 2
    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](http://pages.cs.wisc.edu/~rkennedy/exact-float?number=44.556677) – David Heffernan Dec 01 '11 at 17:30
  • @DavidHeffernan How many meaningful digits are there? – Roman Byshko Dec 01 '11 at 17:33
  • Now is an appropriate time to bring up "Floating Point Numbers Aren't Real": http://www.freshsources.com/FPNotReal.pdf – Kennet Belenky Dec 01 '11 at 17:41
  • @roman for single precision it's around 7 sig digits – David Heffernan Dec 01 '11 at 18:07
  • Your title doesn't match your content well. Your content asks for 6 decimal places, but your title doesn't say anything about that – barlop Nov 29 '17 at 15:43
  • 11
    @barlop Six years later, I think I'll live with it man – Roman Rdgz Nov 29 '17 at 15:52

7 Answers7

209

You can do it like this:

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

6 represents the number of digits after the decimal separator.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
Roman Byshko
  • 8,591
  • 7
  • 35
  • 57
  • Thanks! I though of it, but I just thought It couldn't be so easy! – Roman Rdgz Dec 01 '11 at 17:38
  • 9
    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. – caf Dec 02 '11 at 00:37
34

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 4
    `%09.6f` might also be an option. – caf Dec 02 '11 at 00:36
  • 2
    @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 ? – xyz Apr 12 '16 at 08:53
  • And one of the website https://www.codingunit.com/printf-format-specifiers-format-conversions-and-formatted-output explaining ` %3.2f ` as (print as a floating point at least 3 wide and a precision of 2) . So I am confuse now – xyz Apr 12 '16 at 09:22
  • @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. – Tino Jun 19 '21 at 08:42
9
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.

J0e3gan
  • 8,740
  • 10
  • 53
  • 80
rohit
  • 321
  • 4
  • 9
8
printf("%.<number>f", myFloat) //where <number> - digit after comma

https://cplusplus.com/reference/cstdio/printf/

Daryl Wright
  • 81
  • 1
  • 8
Ihor Shubin
  • 10,628
  • 3
  • 25
  • 33
  • 4
    be carefull! the **f** specifier have to be after the number of digits so `printf("%.numberf", myFloat)` Tank you for the useful link! – Niles May 03 '16 at 14:03
5

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
2

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

Pang
  • 9,564
  • 146
  • 81
  • 122
Charlie Brown
  • 144
  • 10
0

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

Shankar Raju
  • 4,356
  • 6
  • 33
  • 52
  • 2
    It isn't correct. Format "%2.6f" means total width of 2 characters with 6 characters after decimal point. – Valeriy Van Jul 30 '13 at 18:39
  • 1
    @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 – Tino Jun 19 '21 at 09:01
  • @Tino I was thinking %0.3f would print 0 digits in total - that also doesn't make sense :D – fahd Jan 09 '23 at 16:17