0

Firstly i'm new to coding so go easy please.

I have a web flask app with sqlalchemy using mysql.

I have the db code working fine but in one column i wish to use float. However after declaring my db.column as float or even numeric as reading up suggests the data is always rounded up or down.

I wish to save as is. IE always keeps the decimal point if there is one

i have this as my class:

`class crypto_buys_tb(db.Model): # database table for crypto buys id = db.Column(db.Integer, primary_key=True) crypto_name = db.Column(db.String(100)) # these are the db columns and datatypes for columns crypto_price = db.Column(db.FLOAT) amount_paid = db.Column(db.Integer)

def __init__(self, crypto_name, crypto_price, amount_paid):  # when a class instance is created, params needed
    self.crypto_name = crypto_name
    self.crypto_price = crypto_price
    self.amount_paid = amount_paid  `

when entering '15.6' as the 'crypto price'

It rounds off as below

enter image description here

i wish to keep it as entered without rounding. IE '15.6'

thanks for your time and help

here is the output from the mysql shell:

shell output

bully79
  • 23
  • 4
  • 2
    Can you [edit] the question to include the output of these commands in the MySQL command line shell: `DESCRIBE table_name` and `SELECT * FROM table_name` where "table_name" is the name of your table. Please paste the output as text, not an image. – snakecharmerb Sep 18 '22 at 12:28
  • So your model used to specify `crypto_price = db.Column(db.Integer)` and then you changed it to `crypto_price = db.Column(db.FLOAT)` ? – Gord Thompson Sep 18 '22 at 12:43
  • yes that's correct. None of these data types stop the rounding of the output in the table of the db. As displayed in the image from sql workbench. As mentioned in the example i used if i enter 15.6 i want it to stay that way, and not be rounded to 16. Thanks – bully79 Sep 18 '22 at 12:51
  • 1
    In the database, `crypto_price` is an integer type. SQLAlchemy doesn't amend column details once they have been created. You need to drop the table and recreate it with the correct column type, or update the column manually with an `ALTER TABLE ...` statement. – snakecharmerb Sep 18 '22 at 13:20
  • ah i see so i can't amend on the fly?. What type would i need to use so i can enter both integer and floating type without rounding please?. The reason being it's a crypto calculator and i could for example be purchasing a token using an integer ($10) for example. Another purchase could be ($10.50) .Thank you so much – bully79 Sep 18 '22 at 13:28
  • For currency column's I'd use [DECIMAL](https://docs.sqlalchemy.org/en/14/dialects/mysql.html#sqlalchemy.dialects.mysql.DECIMAL) or `DOUBLE`. I think either will accept integer input values, the return values will be [Decimal](https://docs.python.org/3.10/library/decimal.html#decimal.Decimal) instances. – snakecharmerb Sep 18 '22 at 13:37
  • brill thanks!. Do i need to use as is for example db.Column(db.DECIMAL) without params as well?. thanks again for your help – bully79 Sep 18 '22 at 13:46
  • https://stackoverflow.com/q/3730019/2144390 – Gord Thompson Sep 18 '22 at 13:51
  • Thanks guys i used this rypto_price = db.Column(db.DECIMAL(6, 2)) . I dropped table and started again with this and it works. Thanks again – bully79 Sep 18 '22 at 14:45

0 Answers0