本文介绍了浮点限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

  a ='2.3'

我想要显示 a 作为浮点数值。



由于 a 是一个字符串,我试过了:

  float (a)

我得到的结果是:

  2.2999999999999998 

我想为此问题提供解决方案。请帮助我。



我正在关注。我认为它更多地反映了你对浮点类型的理解,而不是对Python的理解。请参阅(基于.NET,但仍然相关)这个不准确背后的原因。如果您需要保留精确的十进制表示形式,则应使用。


My code:

a = '2.3'

I wanted to display a as a floating point value.

Since a is a string, I tried:

float(a)

The result I got was :

2.2999999999999998

I want a solution for this problem. Please, kindly help me.

I was following this tutorial.

解决方案

I think it reflects more on your understanding of floating point types than on Python. See my article about floating point numbers (.NET-based, but still relevant) for the reasons behind this "inaccuracy". If you need to keep the exact decimal representation, you should use the decimal module.

这篇关于浮点限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 08:55