将浮点数转换为字符串而不进行四舍五入

发布于 2024-08-02 04:48:09 字数 176 浏览 6 评论 0原文

我正在编写一个程序,由于不需要解释的原因,需要将浮点数转换为字符串以便使用 len() 进行计数。然而,str(float(x)) 会导致 x 在转换为字符串时被四舍五入,这会导致整个事情失败。有谁知道修复它吗? 如果您想知道,请使用以下代码:

len(str(float(x)/3))

I'm making a program that, for reasons not needed to be explained, requires a float to be converted into a string to be counted with len(). However, str(float(x)) results in x being rounded when converted to a string, which throws the entire thing off. Does anyone know of a fix for it?
Here's the code being used if you want to know:

len(str(float(x)/3))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱要勇敢去追 2024-08-09 04:48:09

在处理浮点数时,某种形式的舍入通常是不可避免的。这是因为可以精确地以 10 为基数表达的数字并不总是能够精确地以 2 为基数(计算机使用的基数)表达。

例如:

>>> .1
0.10000000000000001

在本例中,您会看到使用 repr 将 .1 转换为字符串:

>>> repr(.1)
'0.10000000000000001'

我相信当您使用 str() 时,Python 会砍掉最后几位数字以解决此问题,但这只是一个部分解决方法,并不能替代对正在发生的事情的理解。

>>> str(.1)
'0.1'

我不确定“舍入”到底给您带来了什么问题。也许您会更好地使用字符串格式来更精确地控制输出?

例如

>>> '%.5f' % .1
'0.10000'
>>> '%.5f' % .12345678
'0.12346'

此处的文档

Some form of rounding is often unavoidable when dealing with floating point numbers. This is because numbers that you can express exactly in base 10 cannot always be expressed exactly in base 2 (which your computer uses).

For example:

>>> .1
0.10000000000000001

In this case, you're seeing .1 converted to a string using repr:

>>> repr(.1)
'0.10000000000000001'

I believe python chops off the last few digits when you use str() in order to work around this problem, but it's a partial workaround that doesn't substitute for understanding what's going on.

>>> str(.1)
'0.1'

I'm not sure exactly what problems "rounding" is causing you. Perhaps you would do better with string formatting as a way to more precisely control your output?

e.g.

>>> '%.5f' % .1
'0.10000'
>>> '%.5f' % .12345678
'0.12346'

Documentation here.

栖竹 2024-08-09 04:48:09
len(repr(float(x)/3))

但我必须说,这并不像你想象的那么可靠。

浮点数以十进制数形式输入/显示,但您的计算机(实际上是标准 C 库)将它们存储为二进制数。此转换会产生一些副作用:

>>> print len(repr(0.1))
19
>>> print repr(0.1)
0.10000000000000001

本章解释了为什么会发生这种情况python 教程。

解决方案是使用专门跟踪十进制数字的类型,例如 python 的 decimal.Decimal< /代码>

>>> print len(str(decimal.Decimal('0.1')))
3
len(repr(float(x)/3))

However I must say that this isn't as reliable as you think.

Floats are entered/displayed as decimal numbers, but your computer (in fact, your standard C library) stores them as binary. You get some side effects from this transition:

>>> print len(repr(0.1))
19
>>> print repr(0.1)
0.10000000000000001

The explanation on why this happens is in this chapter of the python tutorial.

A solution would be to use a type that specifically tracks decimal numbers, like python's decimal.Decimal:

>>> print len(str(decimal.Decimal('0.1')))
3
乙白 2024-08-09 04:48:09

其他答案已经指出,至少可以说,浮点数的表示是一个棘手的问题。

由于您在问题中没有提供足够的上下文,我不知道十进制模块是否可以满足您的需求:

http://docs.python.org/library/decimal.html

除此之外,您可以明确指定您希望获得的精度(从文档):

>>> getcontext().prec = 6
>>> Decimal('3.0')
Decimal('3.0')
>>> Decimal('3.1415926535')
Decimal('3.1415926535')
>>> Decimal('3.1415926535') + Decimal('2.7182818285')
Decimal('5.85987')
>>> getcontext().rounding = ROUND_UP
>>> Decimal('3.1415926535') + Decimal('2.7182818285')
Decimal('5.85988')

我的提示中的一个简单示例( python 2.6):

>>> import decimal
>>> a = decimal.Decimal('10.000000001')
>>> a
Decimal('10.000000001')
>>> print a
10.000000001
>>> b = decimal.Decimal('10.00000000000000000000000000900000002')
>>> print b
10.00000000000000000000000000900000002
>>> print str(b)
10.00000000000000000000000000900000002
>>> len(str(b/decimal.Decimal('3.0')))
29

也许这可以帮助?
从 2.4 开始,decimal 就出现在 python stdlib 中,并在 python 2.6 中添加了一些内容。

希望这可以帮助,
弗朗西斯科

Other answers already pointed out that the representation of floating numbers is a thorny issue, to say the least.

Since you don't give enough context in your question, I cannot know if the decimal module can be useful for your needs:

http://docs.python.org/library/decimal.html

Among other things you can explicitly specify the precision that you wish to obtain (from the docs):

>>> getcontext().prec = 6
>>> Decimal('3.0')
Decimal('3.0')
>>> Decimal('3.1415926535')
Decimal('3.1415926535')
>>> Decimal('3.1415926535') + Decimal('2.7182818285')
Decimal('5.85987')
>>> getcontext().rounding = ROUND_UP
>>> Decimal('3.1415926535') + Decimal('2.7182818285')
Decimal('5.85988')

A simple example from my prompt (python 2.6):

>>> import decimal
>>> a = decimal.Decimal('10.000000001')
>>> a
Decimal('10.000000001')
>>> print a
10.000000001
>>> b = decimal.Decimal('10.00000000000000000000000000900000002')
>>> print b
10.00000000000000000000000000900000002
>>> print str(b)
10.00000000000000000000000000900000002
>>> len(str(b/decimal.Decimal('3.0')))
29

Maybe this can help?
decimal is in python stdlib since 2.4, with additions in python 2.6.

Hope this helps,
Francesco

路还长,别太狂 2024-08-09 04:48:09

我知道这已经太晚了,但对于那些第一次来这里的人,我想发布一个解决方案。我有一个浮点值index和一个字符串imgfile,我遇到了和你一样的问题。这就是我解决问题的方法

index = 1.0
imgfile = 'data/2.jpg'
out = '%.1f,%s' % (index,imgfile)
print out

输出是

1.0,data/2.jpg

您可以根据您的方便修改此格式示例。

I know this is too late but for those who are coming here for the first time, I'd like to post a solution. I have a float value index and a string imgfile and I had the same problem as you. This is how I fixed the issue

index = 1.0
imgfile = 'data/2.jpg'
out = '%.1f,%s' % (index,imgfile)
print out

The output is

1.0,data/2.jpg

You may modify this formatting example as per your convenience.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文