python - 理解字符串格式中的 Python %g,实现 Java String.format 行为

标签 python python-2.7 string-formatting

在 Java 中:

String test1 = String.format("%7.2g", 3e9);
System.out.println(test1);

这会打印 3.0e+09

在 Python 2.7 中,如果我运行这段代码

for num in [3e9, 3.1e9, 3.01e9, 3e2, 3.1e2, 3.01e2]:
   print '%7.2g %7.2f %7.2e' % (num, num, num)

我明白了

  3e+09 3000000000.00 3.00e+09
3.1e+09 3100000000.00 3.10e+09
  3e+09 3010000000.00 3.01e+09
  3e+02  300.00 3.00e+02
3.1e+02  310.00 3.10e+02
  3e+02  301.00 3.01e+02

嗯?看起来精度 (.2) 参数在 Python 的 %g 中与在 Python 的 %f、Python 的 %e 或Java 的 %g。这是 doc (我的重点):

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.

WTF?有什么办法可以防止删除那些尾随的零吗?字符串格式化的重点是实现某种一致性,例如用于文本对齐。

有没有什么方法可以让 Java 行为(本质上是小数点右边的有效数字位数)而不必从头开始重写整个事情?

最佳答案

使用 format 方法,你可以做这样的事情:

for num in [3e9, 3.1e9, 3.01e9, 3e2, 3.1e2, 3.01e2]:
    print ('{n:7.2{c}} {n:7.2f} {n:7.2e}'.format(n=num, c='e' if num > 1e4 else 'f'))

输出:

3.00e+09 3000000000.00 3.00e+09
3.10e+09 3100000000.00 3.10e+09
3.01e+09 3010000000.00 3.01e+09
 300.00  300.00 3.00e+02
 310.00  310.00 3.10e+02
 301.00  301.00 3.01e+02

它有两个部分可能不太为人所知。

1。参数化字符串格式

除了简单的格式化:

>>> '{}'.format(3.5)
'3.5'

以及对结果进行更详细规范的格式化:

>>> '{:5.2f}'.format(3.5)
' 3.50'

您可以使用可以在字符串中访问的格式关键字参数:

>>> '{num:5.2f}'.format(num=3.5)
' 3.50'

您也可以将这些用于格式规范本身:

>>> '{:5.{deci}f}'.format(3.5, deci=3)
'3.500'

2。 if 表达式

除了 if 语句之外,还有一个 if 表达式,又名 ternary operator .

所以,这个表达式:

a = 1
b = 2
res = 10 if a < b else 20

等同于这个语句:

if a < b:
    res = 10
else:
    res= 20

将两者放在一起会产生如下结果:

'{num:7.2{c}}'.format(num=num, c='e' if num > 1e4 else 'f')

关于python - 理解字符串格式中的 Python %g,实现 Java String.format 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41840613/

相关文章:

python - Odoo:创建新报价时如何更改默认搜索行为?

c# - 包含数字 .ToString() 格式的 HTML

java格式字符串 "%,d"和 "%, d"

python - Tensorflow:未启用任何 MLIR 优化 channel (注册 1)

python - 如何通过 python-eve 将图像上传到一些外部存储服务器,例如S3?

python - 如何在scrapy中给出每个请求之间的延迟?

python - 停止在 python 中打印 'no matches'

python - DAG 在 Google Cloud Composer 网络服务器上不可点击,但在本地 Airflow 上运行良好

python - pytest 找不到模块

python - 在Python中读取格式化的多行