Home > Software design >  Record field inserting 1000000000.0 instead 999999999 Flask-SQLAlchemy MySQL
Record field inserting 1000000000.0 instead 999999999 Flask-SQLAlchemy MySQL

Time:12-09

Table structure image

The column amount of table is type of Float datatype.

when I'm inserting a new record with the amount value 999999999 it gets automatically converted to 1000000000.0

I'm using Python3.10.2, Flask 2.2.2, Flask-SQLAlchemy 3.0.2, Flask-Migrate 4.0.0 and MySQL Database system

I tried both negative and positive values in negative the result is same but with negative sign

I want to insert 999999999 value as it is in the table.

CodePudding user response:

I think the data type needs to be in the right format maybe an Int64 or 32 as opposed to a float, currency or decimal type. Note this answer: The FLOAT and DOUBLE types represent approximate numeric data values. MySQL uses four bytes for single-precision values .. Why does MySQL round floats way more than expected?

CodePudding user response:

FLOAT datatype is an approximate numeric data value. You may experience rounding errors as it is uses four bytes for the data (DOUBLE uses eight bytes).

You should consider using DECIMAL datatype instead.

See a dbfiddle

  • Related