The answer to the question is correct, however, some users will stumble on this question to find out the difference between DecimalField and FloatField. The floating-point rounding problem that Seth brings up is a problem for the currency.
Django Member States
The FloatField class is sometimes mixed with the DecimalField class. Although both of them represent real numbers, they represent these numbers in different ways. FloatField uses the Pyatons float type internally, and DecimalField uses the Pythons decimal type. More details here .
The following are other differences between the two fields:
DecimalField:
- Decimal fields must define the attributes "decimal_places" and "max_digits".
- You get two free form checks included here from the above required attributes, that is, if you set max_digits to 4 and enter a decimal number equal to 4.00000 (5 digits), you will get this error: make sure there are no more than 4 digits.
- You will also get a similar form check for decimal places (which in most browsers will also be checked on the front using the step attribute in the input field. If you set decimal_places = 1 and enter the value 0.001 as the value, you will get an error that the minimum value should be 0.1.
- Returns a decimal character. Decimal type -
- Has no additional validation as DecimalField
- With the Decimal type, rounding is also handled for you due to the necessary attributes that need to be set as described above. So, from the shell, if you
- In the database (postgresql) DecimalField is saved as a number (max_digits, decimal_laces) Type and Storage are set as "main", from the above example, the Type is numeric (4.1)
Learn more about DecimalField from Django Docs .
FloatField:
- Returns the built-in type float,
- There is no intellectual rounding and can actually lead to rounding problems, as described in the Setes answer.
- Has no additional form validation that you get from DecimalField
- In the database (postgresql), the FloatField is saved as a "double precision" type, and Storage is set as "normal"
Learn more about FloatField from Django Docs .
Applies to both:
- Both fields extend from the Field class and can take blank, null, verbose_name, name, primary_key, max_length, unique, db_index, rel, default , 'unique_for_date', 'unique_for_year', 'options', 'help_text', 'db_column', 'db_tablespace', 'auto_created', 'validators', 'error_messages', since all fields that extend from the "field" will have .
- The default widget for both fields is TextInput.
I came across this question when I was looking for the difference between the two fields, so I think this will help those who are in the same situation :)
UPDATE. To answer the question, I think you can either leave or represent the currency, although Decimal is much better suited. There is a rounding issue when it expects to swim, so you need to use round(value, 2) so that your floating point representation is rounded to two decimal places. Here is a quick example:
>>> round(1.13 * 50 + .01, 2) 56.51
You may still have problems with the float and round. As here, we see that it is rounded by 5:
>>> round(5.685, 2) 5.68
But in this case it will be rounded:
>>> round(2.995, 2) 3.0
This is all related to how the float is stored in memory. See here .
radtek Dec 12 '14 at 16:44 2014-12-12 16:44
source share