Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-67790: Add float-style formatting for Fraction objects #1

Closed
wants to merge 17 commits into from

Conversation

mdickinson
Copy link
Owner

@mdickinson mdickinson commented Dec 3, 2022

This work-in-progress / proof-of-concept PR adds float-style formatting for fractions.Fraction objects.

So far this includes support for the 'e', 'f', 'g', and '%' presentation types. The 'n' presentation type is not yet supported.

Python 3.12.0a2+ (heads/fraction-format:8cf8d31ce7, Dec  3 2022, 17:24:40) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from fractions import Fraction as F
>>> f = F(22, 7)
>>> format(f, 'f')
'3.142857'
>>> format(f, 'e')
'3.142857e+00'
>>> f'{f:f}'  
'3.142857'
>>> format(f, '.50f')
'3.14285714285714285714285714285714285714285714285714'
>>> format(F('123456789.12345678'), '_.6f')
'123_456_789.123457'
>>> format(f, '020,f')
'0,000,000,003.142857'
>>> format(f, 'X^20f')
'XXXXXX3.142857XXXXXX'
>>> format(F(2**10000), 'e')
'1.995063e+3010'

It supports all reasonable bells and whistles of the formatting mini-language.

@mdickinson
Copy link
Owner Author

Updated to add '%' formatting, and to add the 'F' format specifier as an alias of 'f'.

@mdickinson mdickinson changed the title bpo-67790: Add '.f' formatting for Fraction objects bpo-67790: Add float-style formatting for Fraction objects Dec 4, 2022
Lib/fractions.py Outdated Show resolved Hide resolved
@mdickinson
Copy link
Owner Author

PR recreated against upstream: python#100161. Closing here.

@mdickinson mdickinson closed this Dec 10, 2022
hugovk pushed a commit that referenced this pull request Sep 7, 2023
…es (#1… (python#108688)

This reverts commit 08447b5.

Revert also _ctypes.c changes of the PyDict_ContainsString() change,
commit 6726626.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant