## [Python]从教程复制的代码,我这里显示的结果和教程中不同?

Python/PHP/Perl 开发与设计

### [Python]从教程复制的代码,我这里显示的结果和教程中不同?

[Python]从教程复制的代码,我这里显示的结果和教程中不同?

https://docs.python.org/3.4/tutorial/in ... formatting

``````>>> import math
>>> print('The value of PI is approximately {}.'.format(math.pi))
The value of PI is approximately 3.14159265359.
>>> print('The value of PI is approximately {!r}.'.format(math.pi))
The value of PI is approximately 3.141592653589793.``````

``````Python 3.4.2 (default, Oct  8 2014, 13:14:40)
[GCC 4.9.1] on linux
>>> import math
>>> print('The value of PI is approximately {}.'.format(math.pi))
The value of PI is approximately 3.141592653589793.
>>>
>>> print('The value of PI is approximately {!r}.'.format(math.pi))
The value of PI is approximately 3.141592653589793.
>>>
``````
poloshiao

### Re: [Python]从教程复制的代码,我这里显示的结果和教程中不同?

1. 猜測
format(math.pi)
Floating Point Arithmetic
precision

2. 參閱
2-1. https://docs.python.org/3.4/library/math.html
9.2.7. Constants
math.pi
The mathematical constant π = 3.141592..., to available precision.
2-2. https://docs.python.org/3.4/tutorial/fl ... -fp-issues
Floating Point Arithmetic: Issues and Limitations

astolia

### Re: [Python]从教程复制的代码,我这里显示的结果和教程中不同?

python 3.2之前，str()转换数值默认的精度是12。教程偷懒没更新，直接拿原来版本的例子来用了。