-3

How show 00156,00

float number = 156;
printf("%5.2f",number);

The output of the example above:

Output: 156.00

Herick
  • 217
  • 1
  • 3
  • 13
  • 7
    C or C++, pick one because the answer will depend on the language. – Borgleader Sep 29 '16 at 18:01
  • The reason it printed 6 characters not the 5 requested is that this format does not break the value supplied. With 2 decimal places the value cannot be printed with 5 characters. – Weather Vane Sep 29 '16 at 18:09
  • Is this a [duplicate question](http://stackoverflow.com/questions/14753505/c-c-printf-use-commas-instead-of-dots-as-decimal-separator) or of [this one](http://stackoverflow.com/questions/7684014/c-printf-with-f-but-localized-for-the-users-country)? – Weather Vane Sep 29 '16 at 18:13
  • Possible duplicate of [Printing leading 0's in C?](http://stackoverflow.com/questions/153890/printing-leading-0s-in-c) – Peter VARGA Sep 29 '16 at 19:03

1 Answers1

4

You need to pass the 0 flag for leading zeros, and the field width must be wide enough to have any leading characters printed. So you might try this:

printf("%08.2f", number);

The . verses , (aka, the "radix character") is determined from the LC_NUMERIC portion of the locale. See man setlocale for information on setting this programatically.

Chris
  • 451
  • 2
  • 5