如何处理在Python SQLite的财务计算浮动类型舍入错误? [英] How to deal with rounding errors of floating types for financial calculations in Python SQLite?

查看:142
本文介绍了如何处理在Python SQLite的财务计算浮动类型舍入错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个财务应用程序,似乎我在sqlite中的浮动正在浮动。有时候4.0会是4.000009,6.0会是6.00006,就是这样。我怎样才能使这些更精确,而不会影响我的财务计算?

值来自Python,如果重要的话。不知道哪个地区的数字是来自。

解决方案

这是使用SQLite的常见问题,因为它没有货币类型。 .Mark说,您可以使用 Decimal 表示库。然而,SQLite 3只支持二进制浮点数(sqlite类型REAL),所以你将不得不将十进制编码的浮点存储为TEXT或BLOB或转换为REAL(但你会回到一个64位的二进制浮点数) >
所以考虑一下你需要表示的数字范围,以及是否需要能够在数据库中执行计算。



你可能会更好关闭使用不同的支持NUMERIC类型的数据库,例如 MYSQL PostgreSQL 火鸟


I'm creating a financial app and it seems my floats in sqlite are floating around. Sometimes a 4.0 will be a 4.000009, and a 6.0 will be a 6.00006, things like that. How can I make these more exact and not affect my financial calculations?

Values are coming from Python if that matters. Not sure which area the messed up numbers are coming from.

解决方案

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

这篇关于如何处理在Python SQLite的财务计算浮动类型舍入错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆