Django - How to allow users to enter decimals in scientific notation?
I want users to be able to enter a standard decimal value like 1.51 or enter a value in scientific notation like 4.08E+13.
I set up a decimal field in my model but when I try to enter the above example I get this开发者_StackOverflow社区 validation message:
"Ensure that there are no more than 3 decimal places."
Side note:
I did notice that Python already supports this format:
>>> from decimal import Decimal
>>> print Decimal("4.08E+13")
4.08E+13
So maybe it won't be too hard to set something up in Django?
This looks like a Django bug, DecimalField.validate() is wrong for positive exponents:
>>> from django.forms import DecimalField
>>> f = DecimalField(max_digits=10, decimal_places=1)
>>> f.validate(Decimal('1E+2'))
Traceback (most recent call last):
...
ValidationError: [u'Ensure that there are no more than 1 decimal places.']
You may want to file a bug.
精彩评论