Title: Improve `printf`-style formatting docs for non-built-in types · Issue #142108 · python/cpython · GitHub
Open Graph Title: Improve `printf`-style formatting docs for non-built-in types · Issue #142108 · python/cpython
X Title: Improve `printf`-style formatting docs for non-built-in types · Issue #142108 · python/cpython
Description: Add a description about how Decimal behaves differently with format and % formatting in https://docs.python.org/3/library/string.html#format-specification-mini-language For __format__, Decimal is formatted with the highest possible preci...
Open Graph Description: Add a description about how Decimal behaves differently with format and % formatting in https://docs.python.org/3/library/string.html#format-specification-mini-language For __format__, Decimal is f...
X Description: Add a description about how Decimal behaves differently with format and % formatting in https://docs.python.org/3/library/string.html#format-specification-mini-language For __format__, Decimal is f...
Opengraph URL: https://github.com/python/cpython/issues/142108
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Improve `printf`-style formatting docs for non-built-in types","articleBody":"# Add a description about how Decimal behaves differently with __format__ and % formatting in https://docs.python.org/3/library/string.html#format-specification-mini-language\n\nFor \\_\\_format\\_\\_, Decimal is formatted ~~with the highest possible precision~~ as it is.\n\nFor % formatting, Decimal is converted to float before formatting.\n\nThese behaviors have not changed since they were added.\n- The Decimal class was added in Python 2.4 (https://docs.python.org/2.4/whatsnew/whatsnew24.html).\n- The \\_\\_format\\_\\_ method was added in Python 2.6 (https://docs.python.org/2.6/whatsnew/2.6.html).\n\nCompare:\n| `sys.version` | `'%f' % 11111111111111111111.0` | `'%f' % Decimal('11111111111111111111.0')` | `Decimal('11111111111111111111.0').__format__('f')` | `'%e' % 0.0` | `'%e' % Decimal('0.0')` | `Decimal('0.0').__format__('e')` | `'%.20e' % 11111111111111111111.0` | `'%.20e' % Decimal('11111111111111111111.0')` | `Decimal('11111111111111111111.0').__format__('.20e')` | `'%.20g' % 11111111111111111111.0` | `'%.20g' % Decimal('11111111111111111111.0')` | `Decimal('11111111111111111111.0').__format__('.20g')` |\n| ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: |\n| '2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)]' | '11111111111111111000.000000' | '11111111111111111000.000000' | Exception | '0.000000e+000' | '0.000000e+000' | Exception | '1.11111111111111110000e+019' | '1.11111111111111110000e+019' | Exception | '11111111111111111000' | '11111111111111111000' | Exception |\n| '2.6.6 (r266:84297, Aug 24 2010, 18:13:38) [MSC v.1500 64 bit (AMD64)]' | '11111111111111111000.000000' | '11111111111111111000.000000' | '11111111111111111111.0' | '0.000000e+00' | '0.000000e+00' | '0e-1' | '1.11111111111111110000e+19' | '1.11111111111111110000e+19' | '1.11111111111111111110e+19' | '11111111111111111000' | '11111111111111111000' | '11111111111111111111' |\n| '2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)]' | '11111111111111110656.000000' | '11111111111111110656.000000' | '11111111111111111111.0' | '0.000000e+00' | '0.000000e+00' | '0e-1' | '1.11111111111111106560e+19' | '1.11111111111111106560e+19' | '1.11111111111111111110e+19' | '11111111111111110656' | '11111111111111110656' | '11111111111111111111' |\n| '3.14.0 (tags/v3.14.0:ebf955d, Oct 7 2025, 10:06:33) [MSC v.1944 32 bit (Intel)]' | '11111111111111110656.000000' | '11111111111111110656.000000' | '11111111111111111111.0' | '0.000000e+00' | '0.000000e+00' | '0e-1' | '1.11111111111111106560e+19' | '1.11111111111111106560e+19' | '1.11111111111111111110e+19' | '11111111111111110656' | '11111111111111110656' | '11111111111111111111' |\n| '3.14.0 (tags/v3.14.0:ebf955d, Oct 7 2025, 10:15:03) [MSC v.1944 64 bit (AMD64)]' | '11111111111111110656.000000' | '11111111111111110656.000000' | '11111111111111111111.0' | '0.000000e+00' | '0.000000e+00' | '0e-1' | '1.11111111111111106560e+19' | '1.11111111111111106560e+19' | '1.11111111111111111110e+19' | '11111111111111110656' | '11111111111111110656' | '11111111111111111111' |\n| '3.15.0a2 (tags/v3.15.0a2:a625628, Nov 18 2025, 18:12:01) [MSC v.1944 64 bit (AMD64)]' | '11111111111111110656.000000' | '11111111111111110656.000000' | '11111111111111111111.0' | '0.000000e+00' | '0.000000e+00' | '0e-1' | '1.11111111111111106560e+19' | '1.11111111111111106560e+19' | '1.11111111111111111110e+19' | '11111111111111110656' | '11111111111111110656' | '11111111111111111111' |\n\n```python\n# ./test.py\nimport sys\nfrom decimal import Decimal\n\nfor expr in (\n \"sys.version\",\n\n \"'%f' % 12345123451234512345.0\",\n \"'%f' % Decimal('12345123451234512345.0')\",\n \"Decimal('12345123451234512345.0').__format__('f')\",\n\n \"'%e' % 0.0\",\n \"'%e' % Decimal('0.0')\",\n \"Decimal('0.0').__format__('e')\",\n\n \"'%.20e' % 12345123451234512345.0\",\n \"'%.20e' % Decimal('12345123451234512345.0')\",\n \"Decimal('12345123451234512345.0').__format__('.20e')\",\n\n \"'%.20g' % 12345123451234512345.0\",\n \"'%.20g' % Decimal('12345123451234512345.0')\",\n \"Decimal('12345123451234512345.0').__format__('.20g')\",\n):\n print(expr)\n try:\n print(repr(eval(expr)))\n except Exception:\n print('Exception')\n print('')\n```\n\n```python\n# ./test_runner.py\nfrom pathlib import Path\nfrom subprocess import run, PIPE\n\nPYTHON_DIR_ROOT = r\"C:\\python_dir_root\"\n\ndef print_row(row, file):\n for v in row: print(f\"| {v} \", end='', file=file)\n print(\"|\", file=file)\n\ndef print_md_table(headers, rows, file):\n print_row(headers, file=file)\n print_row(['---:'] * len(headers), file=file)\n for row in rows: print_row(row, file=file)\n print(file=file)\n\nrows = []\nfor dir in Path(PYTHON_DIR_ROOT).iterdir():\n out = run([str(dir/\"python.exe\"), \"./test.py\"], stdout=PIPE).stdout\n out = out.decode('utf-8').splitlines()\n headers = [f\"`{h}`\" for h in out[::3]]\n rows.append(out[1::3])\n\nwith open(\"./test_result.md\", 'w', encoding='utf-8') as fp:\n print_md_table(headers, rows, file=fp)\n```\n\n\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-142117\n* gh-142126\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/1nftf","@type":"Person","name":"1nftf"},"datePublished":"2025-11-30T08:54:58.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":18},"url":"https://github.com/142108/cpython/issues/142108"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:866acf49-83e3-117d-d007-d5b78e99a078 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B966:CED76:5EC224:837AA7:696A78BE |
| html-safe-nonce | a8a77bd0df3a2b1fa8064af5aebeeb6899f32b8dd3a66a6a303a22862dc64cdf |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCOTY2OkNFRDc2OjVFQzIyNDo4MzdBQTc6Njk2QTc4QkUiLCJ2aXNpdG9yX2lkIjoiNTM0MzgwNzgyMDUwOTM3ODc1MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 5502447cc6256aa1e692a2b89ce41449be06f096c552fa95b7201aca40904e07 |
| hovercard-subject-tag | issue:3677538450 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/142108/issue_layout |
| twitter:image | https://opengraph.githubassets.com/da80d1e4e7c2e3b0cb1531f4ea4c3b02d7907b059a4de34a56ae824a6d2e76a3/python/cpython/issues/142108 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/da80d1e4e7c2e3b0cb1531f4ea4c3b02d7907b059a4de34a56ae824a6d2e76a3/python/cpython/issues/142108 |
| og:image:alt | Add a description about how Decimal behaves differently with format and % formatting in https://docs.python.org/3/library/string.html#format-specification-mini-language For __format__, Decimal is f... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | 1nftf |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5b774e44f85c14a75886edd04ddda4e5a25ddebbb241bcbb590b08a3048730e8 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | cc5f4eee261b3601c1e98e217ceaf28508b9567e |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width