From a87189fc5e2e874219d20700cf811345138dd365 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Wed, 3 May 2017 11:40:09 +0200 Subject: Fixed #28164 -- Improved float conversions in DecimalField.to_python Thanks Tim Graham and Adam Johnson for the reviews. --- tests/model_fields/test_decimalfield.py | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'tests/model_fields') diff --git a/tests/model_fields/test_decimalfield.py b/tests/model_fields/test_decimalfield.py index 2f4540c81d..5163d8188b 100644 --- a/tests/model_fields/test_decimalfield.py +++ b/tests/model_fields/test_decimalfield.py @@ -15,6 +15,12 @@ class DecimalFieldTests(TestCase): f = models.DecimalField(max_digits=4, decimal_places=2) self.assertEqual(f.to_python(3), Decimal('3')) self.assertEqual(f.to_python('3.14'), Decimal('3.14')) + # to_python() converts floats and honors max_digits. + self.assertEqual(f.to_python(3.1415926535897), Decimal('3.142')) + self.assertEqual(f.to_python(2.4), Decimal('2.400')) + # Uses default rounding of ROUND_HALF_EVEN. + self.assertEqual(f.to_python(2.0625), Decimal('2.062')) + self.assertEqual(f.to_python(2.1875), Decimal('2.188')) with self.assertRaises(ValidationError): f.to_python('abc') -- cgit v1.3