如何截断python中的浮点不精确度

编程入门 行业动态 更新时间:2024-10-23 14:27:28
本文介绍了如何截断python中的浮点不精确度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在编写一个程序,对于该程序,比较和排列日期序列中的值很重要.但是,我遇到了浮子不精确的问题

I am writing a program for which it is important to compare and rank values in a date series. However, I am running into problems with the imprecision of floats

我正在从我的SQL Server中提取这些应该都是1.6的数据.但是,事实证明它们略有不同(请参见下文).因此,当我使用dataframe.rank()时,它不会将这两个日期视为相同的等级,而是将02/01/2005之上的01/02/2004排名.

I am pulling these data from my SQL server that are both supposed to be 1.6. However, they turn out to be slightly different (see below). Therefore, when I use dataframe.rank(), it doesn't treat these two dates as the same rank, but rather ranks 01/02/2004 above 02/01/2005.

任何人都不知道该如何处理,以便使这两个人的排名相同?

Anyone have any idea how to deal with this so that these two would end up on the same rank?

modelInputData.loc['01/02/2004',('Level','inflationCore','EUR')] Out[126]: 1.6000000000000003 modelInputData.loc['02/01/2005',('Level','inflationCore','EUR')] Out[127]: 1.6000000000000001

推荐答案

我建议您像银行家那样做-使用美分和整数而不是EUR/USD和浮点数/小数变量

I would recommend you to do it as bankers do - use cents and integers instead of EUR/USD and float/decimal variables

在MySQL方面将其转换为美分,或在熊猫中进行转换:

either convert it to cents on the MySQL side or do it in pandas:

df['amount'] = round(df['amount']*100)

那么您将遇到的问题要少得多

You'll have much less problems then

更多推荐

如何截断python中的浮点不精确度

本文发布于:2023-11-27 04:28:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636594.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:浮点   精确度   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!