How to deal with rounding errors of floating types for financial calculations in Python SQLite? [duplicate] How to deal with rounding errors of floating types for financial calculations in Python SQLite? [duplicate] sqlite sqlite

How to deal with rounding errors of floating types for financial calculations in Python SQLite? [duplicate]


Seeing as this is a financial application, if you only have calculations up to 2 or 3 decimal places, you can store all the data internally as integers, and only convert them to float for presentation purposes.

E.g.

6.00 -> 6004.35 -> 435


This is a common problem using SQLite as it does not have a Currency type.
As S.Mark said you can use the Decimal representation library. However SQLite 3 only supports binary floating point numbers (sqlite type REAL) so you would have to store the Decimal encoded float as either TEXT or a BLOB or convert to REAL(but then you'd be back to a 64bit binary float)
So consider the range of numbers that you need to represent and whether you need to be able to perform calculations from within the Database.

You may be better off using a different DB which supports NUMERIC types e.g. MYSql, PostgreSQL, Firebird