python-3.x print语句中的冒号和点是什么意思?

0mkxixxg  于 2023-01-22  发布在  Python
关注(0)|答案(2)|浏览(202)

.:在print语句中做什么?
例如:

print(f'The estimated revenue for a $350 film is around ${revenue_estimate:.10}.')

这将返回:

The estimated revenue for a $350 film is around $600000000.0.

但是如果没有:.,结果是完全一样的!
我找遍了什么也没找到...

krugob8w

krugob8w1#

以下是您的答案:
https://blog.teclado.com/python-formatting-numbers-for-printing/
我也在研究同样的问题,也把我弄糊涂了。
在冒号后面,指定要打印的有效数字位数。由于您的答案60000000.0正好有10个有效数字,因此将打印整个结果。如果您将代码更改为:print(f'The estimated revenue for a $350 film is around ${revenue_estimate:.9}.'),您将看到以指数表示法表示的输出6e+08

cbjzeqam

cbjzeqam2#

我相信这种表示法指定了输入的类型,在本例中,它是一个float,它被削减到10个小数位,因此

revenue_estimate = 4
print(f'The estimated revenue for a $350 film is around ${revenue_estimate:.10}.')

将产生以下结果:

ValueError                                Traceback (most recent call last)
<ipython-input-61-865ae2076242> in <module>()
      1 revenue_estimate = 4
----> 2 print(f'The estimated revenue for a $350 film is around ${revenue_estimate:.10}.')

ValueError: Precision not allowed in integer format specifier

和4.01234567894将被切割为4.0123456789

相关问题