Curious Omission In New-Style Formats

Ian Kelly ian.g.kelly at gmail.com
Mon Jul 11 11:04:22 EDT 2016


On Sun, Jul 10, 2016 at 6:34 PM, Lawrence D’Oliveiro
<lawrencedo99 at gmail.com> wrote:
> On Sunday, July 10, 2016 at 7:22:42 PM UTC+12, Ian wrote:
>> On Sat, Jul 9, 2016 at 11:54 PM, Lawrence D’Oliveiro wrote:
>>> In printf-style formats, you can specify the number of digits for an
>>> integer separately from the field width. E.g.
>>>
>>>     >>> "%#0.5x" % 0x123
>>>     '0x00123'
>>>
>> except perhaps that precision doesn't really make sense for integers
>> in the first place.
>
> Except that it does make sense, as I showed in my example.

Your example showed a 3-digit number being formatted with a requested
precision of 5 digits. The way this was done was by left-padding the
number with 0s until there were 5 digits, but still only 3 digits of
precision. If you truly wanted to format the number with a precision
of 5 digits, it would look like this:

    0x123.00

It may happen to do what you want in the printf-style format, but
calling the field "precision" is at best misleading, and there are
other ways to accomplish the same result.

> <http://man7.org/linux/man-pages/man3/printf.3.html>

Well, str.format does not use the same syntax as printf.



More information about the Python-list mailing list