Curious Omission In New-Style Formats

Michael Torrie torriem at gmail.com
Mon Jul 11 16:06:24 EDT 2016


On 07/11/2016 01:27 PM, Ian Kelly wrote:
> On Mon, Jul 11, 2016 at 12:54 PM, Terry Reedy <tjreedy at udel.edu> wrote:
>> In any case, I think it an improvement to say that '0x00123' has a field
>> width of 7 rather than a 'precision' of 5.
>>
>>>>> '{:#07x}'.format(0x123)  # specifiy field width
>> '0x00123'
>>>>> "%#0.5x" % 0x123  # specify int precision
>> '0x00123'
> 
> It occurs to me now that this does create a challenge if the format is
> meant to support negative numbers as well:
> 
>>>> '%#0.5x' % -0x123
> '-0x00123'
>>>> '{:#07x}'.format(-0x123)
> '-0x0123'

I'm not sure I've ever seen a negative hex number in the wild. Usually
when I view a number in hex I am wanting the raw representation.  -0x123
with a width of 7 would be 0xFFEDD






More information about the Python-list mailing list