Python 2.6.6 中的小数和科学计数法问题

发布于 2024-11-03 11:22:15 字数 750 浏览 1 评论 0原文

我在处理十进制值时遇到困难,在某些情况下需要将其用于算术,而在其他情况下则需要将其用作字符串。具体来说,我有一个速率列表,例如:

rates=[0.1,0.000001,0.0000001]

我使用它们来指定图像的压缩率。我最初需要将这些值作为数字,因为我需要能够对它们进行排序以确保它们按特定顺序排列。我还希望能够将每个值转换为字符串,这样我就可以 1) 将费率嵌入到文件名中,2) 将费率和其他详细信息记录在 CSV 文件中。第一个问题是任何超过 6 位小数的浮点数在转换为字符串时都是科学格式:

>>> str(0.0000001)
'1e-07'

所以我尝试使用 Python 的 Decimal 模块,但它也将一些浮点数转换为科学记数法(似乎与我读过的文档相反) )。例如:

>>> Decimal('1.0000001')
Decimal('1.0000001')
# So far so good, it doesn't convert to scientific notation with 7 decimal places
>>> Decimal('0.0000001')
Decimal('1E-7')
# Scientific notation, back where I started.

我也按照多篇文章中的建议研究了字符串格式,但我没有任何运气。任何建议和指示都会受到这位 Python 新手的赞赏。

I'm having difficulty with decimal values that I need to use for arithmetic in some cases and as strings in others. Specifically I have a list of rates, ex:

rates=[0.1,0.000001,0.0000001]

And I am using these to specify the compression rates for images. I need to initially have these values as numbers because I need to be able to sort them to make sure they are in a specific order. I also want to be able to convert each of these values to strings so I can 1) embed the rate into the filename and 2) log the rates and other details in a CSV file. The first problem is that any float with more than 6 decimal places is in scientific format when converted to a string:

>>> str(0.0000001)
'1e-07'

So I tried using Python's Decimal module but it is also converting some floats to scientific notation (seemingly contrary to the docs I've read). Ex:

>>> Decimal('1.0000001')
Decimal('1.0000001')
# So far so good, it doesn't convert to scientific notation with 7 decimal places
>>> Decimal('0.0000001')
Decimal('1E-7')
# Scientific notation, back where I started.

I've also looking into string formatting as suggested in multiple posts, but I've not had any luck. Any suggestions and pointers are appreciated by this Python neophyte.

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

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

发布评论

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

评论(4

晨敛清荷 2024-11-10 11:22:15

然后您必须指定字符串格式:

["%.8f" % (x) for x in rates]

这将产生 ['0.10000000', '0.00000100', '0.00000010']。也适用于Decimal

You have to specify the string format then:

["%.8f" % (x) for x in rates]

This yields ['0.10000000', '0.00000100', '0.00000010']. Works with Decimal, too.

离不开的别离 2024-11-10 11:22:15
'{0:f}'.format(Decimal('0.0000001'))

以上应该对你有用

'{0:f}'.format(Decimal('0.0000001'))

The above should work for you

三人与歌 2024-11-10 11:22:15

请参阅 % 格式,尤其是浮动点换算:

'e' 浮点指数格式(小写)。 (3)

'E' 浮点指数格式(大写)。 (3)

'f' 浮点十进制格式。 (3)

'F' 浮点十进制格式。 (3)

'g' 浮点格式。如果指数小于 -4 或不小于精度,则使用小写指数格式,否则使用小数格式。 (4)

'G' 浮点格式。如果指数小于 -4 或不小于精度,则使用大写指数格式,否则使用小数格式。 (4)

示例,使用f格式。

>>> ["%10.7f" %i for i in rates]
[' 0.1000000', ' 0.0000010', ' 0.0000001']
>>> 

您还可以使用较新的(从 2.6 开始)str.format()方法:

>>> ['{0:10.7f}'.format(i) for i in rates]
[' 0.1000000', ' 0.0000010', ' 0.0000001']
>>> 

See % formatting, especially the floating point conversions:

'e' Floating point exponential format (lowercase). (3)

'E' Floating point exponential format (uppercase). (3)

'f' Floating point decimal format. (3)

'F' Floating point decimal format. (3)

'g' Floating point format. Uses lowercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise. (4)

'G' Floating point format. Uses uppercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise. (4)

An example, using f format.

>>> ["%10.7f" %i for i in rates]
[' 0.1000000', ' 0.0000010', ' 0.0000001']
>>> 

You can also use the newer (starting 2.6) str.format() method:

>>> ['{0:10.7f}'.format(i) for i in rates]
[' 0.1000000', ' 0.0000010', ' 0.0000001']
>>> 
风渺 2024-11-10 11:22:15

使用 f 字符串:

>>> rates = [0.1, 0.000001, 0.0000008]
>>> [f'{r:.7f}' for r in rates]
['0.1000000', '0.0000010', '0.0000008']

字符串格式 {r:.7f} 表示小数点后使用的位数,在本例中为 7。

Using f-strings:

>>> rates = [0.1, 0.000001, 0.0000008]
>>> [f'{r:.7f}' for r in rates]
['0.1000000', '0.0000010', '0.0000008']

The string format {r:.7f} indicates the number of digits used after the decimal point, which in this case is 7.

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