格式化浮点数

来源:互联网 发布:自己的五年规划 知乎 编辑:程序博客网 时间:2024/05/18 00:19
from math import piformat = 'pi with three decimals:  .%3f'print (format % pi)pi with three decimals: .3.141593

今天学习的时候因为输错了,导致出现了自己无法理解的结果。为什么是.3.141593呢?

format = 'pi with three decimals:  % 3.f'print (format % pi)pi with three decimals:   3

这是另一个错误。

format = 'pi with three decimals: % .3f'print (format % pi)pi with three decimals: 3.142

这是书中正确的例句。

'%.3f' % pi '3.142'

取pi的三位小数

0 0
原创粉丝点击