Python中的浮点

ora*_*nge 9 python floating-point

我正在学习通过学习Python的艰难之路而且我遇到过:

注意数学似乎"错了"?没有分数,只有整数.通过研究什么是"浮点数"来找出原因.

我已经阅读了它的内容:http://docs.python.org/tutorial/floatingpoint.html

我无法弄清楚如何输出浮点数,我已经考虑过使用了round(3 + 3, 2).

这是正确的吗?

Pab*_*blo 8

对于浮点数,您在数字后面写一个句点,然后写一个零(如果它是整数).

像这样:

1.0 <----浮点.

1 <-------整数

这就是python如何解释它们.

if my_answer != your_question:
    print "I did not understand your question. Please rephrase it."
else:
    print "Good luck. Python is fun."
Run Code Online (Sandbox Code Playgroud)

我同意rohit,不需要0.虽然它让初学者更容易.


Ign*_*ams 5

将值传递给float()构造函数将使其成为float可能.

print float(2)
print float('4.5')
Run Code Online (Sandbox Code Playgroud)

使用字符串插值或格式显示它们.

print '%.3f' % 4.53
Run Code Online (Sandbox Code Playgroud)