Skip to content

Accept zero with a positive exponent in DecimalField - #10103

Merged
auvipy merged 2 commits into
encode:mainfrom
Kernel-Hunter:decimal-zero-exponent
Nov 16, 2026
Merged

auvipy merged 2 commits into
encode:mainfrom
Kernel-Hunter:decimal-zero-exponent

Conversation

@Kernel-Hunter

Copy link
Copy Markdown
Contributor

A zero written with a positive exponent is rejected by DecimalField as having too many digits:

>>> serializers.DecimalField(max_digits=5, decimal_places=2).run_validation("0E+3")
ValidationError: Ensure that there are no more than 3 digits before the decimal point.
>>> serializers.DecimalField(max_digits=3, decimal_places=1).run_validation("0E+3")
ValidationError: Ensure that there are no more than 3 digits in total.

0E+3 is just 0. Java BigDecimal.toString() produces this form for a zero with a negative scale, so it does show up in payloads from JVM clients.

validate_precision adds the exponent to the digit count for every value with exponent >= 0. That is right for 2E+2 (200), but zero has no trailing zeros to count. Django DecimalValidator already skips the exponent when the digit tuple is (0,), so a model DecimalField accepts the same value that the serializer field rejects. I applied the same check here.

Added "0E+3" and Decimal("0E+10") to the valid inputs of TestDecimalField. Without the change that test fails with the max_digits error above. tests/test_fields.py and tests/test_model_serializer.py pass, flake8 is clean.

@auvipy
auvipy self-requested a review November 16, 2026 14:19
@auvipy
auvipy merged commit 9e3f0f9 into encode:main Nov 16, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

3 participants