Validate a field of type Float accept zeros after a decimal point in SQLAlchemy

0

I am using the Sqlalchemy orm, when I enter a data of type Float or Numeric does not accept zeros, example:

price = 12.0231 
price = 12.1021
price = 12.0012

The previous ones, if you keep the zeros

The problem is when the zeros go to the last

price = 12.1100 o price = 12.110

The database does not keep zeros

I am declaring it this way:

price = Column(Float((8,4), asdecimal=False), nullable=False)

price = Column(Numeric(8,4), nullable=False)

Any ideas or suggestions?

    
asked by afr 25.02.2018 в 02:17
source

1 answer

0

The ORM or the database will try to store the information in the most efficient way possible.

If you declare a column like this:

price = Column(Numeric(8,4), nullable=False)

These two sentences are equivalent:

price = 1.10
price = 1.1

And therefore will keep it as efficiently as possible. The typemod 4 of the Numeric only indicates the maximum number of decimals that will be stored in the field

I guess what you want is that your numbers always have a certain format, for example four decimals, in the interface that you present to your users. But this is a problem of how you format that number. If for example it is a web application, you can return the number as is if it is a JSON REST API and format it on the client with javascript.

    
answered by 27.11.2018 в 10:36