为什么在格式字符串中%e的行为与%g不同?

2024-05-17 19:43:08 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试过Python3号:

为什么这些格式字符串返回的数字精度不同?在

>>> "%.3e" % 123456789
'1.235e+08' 
>>> "%.3g" % 123456789
'1.23e+08' 

Tags: 字符串格式精度数字python3
3条回答

从python文档中:

'e' Exponent notation. Prints the number in scientific notation using the letter ‘e’ to indicate the exponent. The default precision is 6.


'g'
General format. For a given precision p >= 1, this rounds the number to p significant digits and then formats the result in either fixed-point format or in scientific notation, depending on its magnitude.

The precise rules are as follows: suppose that the result formatted with presentation type 'e' and precision p-1 would have exponent exp. Then if -4 <= exp < p, the number is formatted with presentation type 'f' and precision p-1-exp. Otherwise, the number is formatted with presentation type 'e' and precision p-1. In both cases insignificant trailing zeros are removed from the significand, and the decimal point is also removed if there are no remaining digits following it.

Positive and negative infinity, positive and negative zero, and nans, are formatted as inf, -inf, 0, -0 and nan respectively, regardless of the precision.

A precision of 0 is treated as equivalent to a precision of 1. The default precision is 6.

其他值:

>>> "%.3e" % 123
'1.230e+02'
>>> "%.3g" % 123
'123'
>>> "%.3e" % 1234
'1.234e+03'
>>> "%.3g" % 1234
'1.23e+03'

那么精确性是如何明确规定的。g似乎使用精度作为精度的常规定义,而{}使用小数点后的位数。在

我将首先描述%g的规则。在

我发现@Dale Myers的引文已经很详细地说明了,我建议你看看,但我会在这里给出更清楚的解释。在

指数与精度

在我们开始讨论规则之前,让我们先弄清楚单词exp(科学记数法中使用的指数)和precision(简称为p,这是格式化表达式中使用的精度,%.6e或{}。精度默认为6)。在

通用格式规则

好的,下面是%g(通用格式)的规则:

  • -4 <= exp < p:使用十进制格式
  • exp < -4:使用指数格式
  • exp >= p:使用指数格式
  • 计算精度位数时,结果将四舍五入。在
  • 精度用于限制所有数字,而不仅仅是十进制数字。在

让我们看看下面所有的边缘情况:

>>> gx = '%.6g'
>>> ex = '%.6e'
# -4 <= exp < p
>>> gx % 12345
12345
>>> gx % 0.012345
0.012345
# exp == p
>>> ex % 1234567
1.23456e+06
>>> gx % 1234567
1.23457e+06 # Notic the decimal digits are rounded
# exp == -4
>>> ex % 0.000123456
1.234560e-4
>>> gx % 0.000123456
0.000123456
# exp < -4
>>> ex % 0.0000123456
1.234560e-5
>>> gx % 0.0000123456
1.23456e-5

为什么我们需要%g

如果您现在了解了%g的规则,那么很明显,它试图在%f和{}之间找到一个中间点,以便在格式化数字时,它将使用最适合您的格式,而不是自己决定%f或{}。在

^{} man page

e, E

The double argument is rounded and converted in the style [-]d.ddde±dd where there is one digit before the decimal-point character and the number of digits after it is equal to the precision; if the precision is missing, it is taken as 6; if the precision is zero, no decimal-point character appears. An E conversion uses the letter E (rather than e) to introduce the exponent. The exponent always contains at least two digits; if the value is zero, the exponent is 00.

...

g, G

The double argument is converted in style f or e (or F or E for G conversions). The precision specifies the number of significant digits. If the precision is missing, 6 digits are given; if the precision is zero, it is treated as 1. Style e is used if the exponent from its conversion is less than -4 or greater than or equal to the precision. Trailing zeros are removed from the fractional part of the result; a decimal point appears only if it is followed by at least one digit.

所以,即使精度相同,它们也会做不同的事情。在

相关问题 更多 >