From 069ea31cfb50cb22c4ae2de0e20340e703150ffa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Hernas?= Date: Mon, 18 May 2015 16:42:24 +0200 Subject: [PATCH 1/2] Added test ensuring DecimalField accepts `2E+2` as 200 and validates decimal place correctly. --- rest_framework/fields.py | 3 ++- tests/test_fields.py | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 96fcc257a2..eecd18cdc6 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -782,7 +782,8 @@ def to_internal_value(self, data): self.fail('invalid') sign, digittuple, exponent = value.as_tuple() - decimals = abs(exponent) + decimals = exponent * decimal.Decimal(-1) if exponent < 0 else 0 + # digittuple doesn't include any leading zeros. digits = len(digittuple) if decimals > digits: diff --git a/tests/test_fields.py b/tests/test_fields.py index 55e9aacc11..568e8d5e7a 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -647,6 +647,7 @@ class TestDecimalField(FieldValues): 0: Decimal('0'), 12.3: Decimal('12.3'), 0.1: Decimal('0.1'), + '2E+2': Decimal('200'), } invalid_inputs = ( ('abc', ["A valid number is required."]), From e8c226c2db3d0c6ce56ad766f2b7212e554bf0ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Hernas?= Date: Mon, 18 May 2015 16:55:40 +0200 Subject: [PATCH 2/2] Fixed lint problems --- rest_framework/fields.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index eecd18cdc6..d8bb0a0179 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -783,7 +783,7 @@ def to_internal_value(self, data): sign, digittuple, exponent = value.as_tuple() decimals = exponent * decimal.Decimal(-1) if exponent < 0 else 0 - + # digittuple doesn't include any leading zeros. digits = len(digittuple) if decimals > digits: