gh-113804: Add 'x' format to float.format()#119945
gh-113804: Add 'x' format to float.format()#119945vstinner wants to merge 1 commit intopython:mainfrom
Conversation
|
Not supported (raise ValueError):
|
| | ``'x'`` | For :class:`float`, hex format. Outputs the number in | | ||
| | | base 16, using lower-case letters for the digits above 9.| | ||
| | | The alternate form adds ``'0x'`` prefix. | |
There was a problem hiding this comment.
| | ``'x'`` | For :class:`float`, hex format. Outputs the number in | | |
| | | base 16, using lower-case letters for the digits above 9.| | |
| | | The alternate form adds ``'0x'`` prefix. | | |
| | ``'x'`` | For :class:`float`, hex format, supported by | | |
| | | :meth:`~float.fromhex`. Represent the number by a | | |
| | | hexadecimal string in the form ``[±][0x]h[.hhh]p±d``, | | |
| | | using using lower-case letters for the digits above 9. | | |
| | | The exponent ``d`` is written in decimal, it always | | |
| | | contains at least one digit, and it gives the power of 2 | | |
| | | by which to multiply the coefficient. | | |
| | | | | |
| | | The alternate form adds ``'0x'`` prefix. | |
- maybe the first sentence + remark about lower-case is enough
- maybe we should document also 1-normalisation & case of subnormals.
Doc/library/string.rst
Outdated
| | | base 16, using lower-case letters for the digits above 9.| | ||
| | | The alternate form adds ``'0x'`` prefix. | | ||
| | | | | ||
| | | Precision and ``'='`` alignment are not supported. | |
There was a problem hiding this comment.
Hmm, what's wrong with =? This seems to be supported on my branch:
>>> f"{-1.1:30x}"
' -1.199999999999ap+0'
>>> f"{-1.1:=30x}"
'- 1.199999999999ap+0'
>>> f"{-1.111111:=30f}"
'- 1.111111'
>>> f"{-1.111111:30f}"
' -1.111111'There was a problem hiding this comment.
I was just too lazy to implement it. If it matters, I can change my implementation to support the "=" alignment as well.
|
Why not simply use |
Do you mean: instead of adding this format? This format gives more formatting options: sign, 0x prefix, alignment. |
|
It is not so difficult to process the result of |
|
@mdickinson @tim-one: What do you think of this approach? Do you agree with @serhiy-storchaka that the existing |
|
@vstinner For me having I do think |
Ok, I rewrote my PR to implement the |
|
It was decided to not pursue this approach, since float.hex() exists and it's enough for most usages, and we don't want to make str.format() more complicated. |
📚 Documentation preview 📚: https://cpython-previews--119945.org.readthedocs.build/