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

Fix small decimals formatting in numberformat.py #18830

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

SurajSanap
Copy link

This ensures that very small numbers (e.g., 1e-200) are formatted as 0.000... instead of using scientific notation.

Trac ticket number

ticket-30363

Branch description

This PR fixes an issue in django/utils/numberformat.py where very small decimals were displayed in exponential notation when a decimal_pos argument was supplied. With the proposed change, such numbers are correctly formatted as 0.00... with the specified precision.

Example:

  • Before: nformat(Decimal('1e-200'), '.', decimal_pos=2) -> 1.00e-200
  • After: nformat(Decimal('1e-200'), '.', decimal_pos=2) -> 0.00

The change introduces a cutoff mechanism that checks whether the number is too small to be represented with the provided decimal positions, and formats it as 0.00... accordingly.

Checklist

  • This PR targets the main branch.
  • The commit message is written in past tense, mentions the ticket number, and ends with a period.
  • I have checked the "Has patch" ticket flag in the Trac system.
  • I have added or updated relevant tests.
  • I have added or updated relevant docs, including release notes if applicable.
  • I have attached screenshots in both light and dark modes for any UI changes.

This ensures that very small numbers (e.g., 1e-200) are formatted as 0.000... instead of using scientific notation.
@SurajSanap
Copy link
Author

Hi maintainers,

The CI test suite reported 5 failures, which appear to be unrelated to my changes. Specifically:

  • The failing tests are in inspectdb, user_commands, validators, and httpwrappers modules.
  • All tests relevant to my changes (utils_tests.test_numberformat) pass successfully both locally and in CI.

Please let me know if there's anything further you'd like me to adjust!

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