Python中的浮点数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4432481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Floating Point in Python
提问by orange
I'm learning via Learn Python The Hard Way and I've come across:
我正在通过 Learn Python The Hard Way 学习,我遇到了:
Notice the math seems “wrong”? There are no fractions, only whole numbers. Find out why by researching what a “floating point” number is.
注意到数学似乎“错误”了吗?没有分数,只有整数。通过研究什么是“浮点”数来找出原因。
I've read what it is on: http://docs.python.org/tutorial/floatingpoint.html
我已经阅读了它的内容:http: //docs.python.org/tutorial/floatingpoint.html
I can't figure out on how I can output floating point numbers, I've thought about using round(3 + 3, 2).
我不知道如何输出浮点数,我想过使用round(3 + 3, 2).
Is this right?
这是正确的吗?
采纳答案by Pablo
For floating point numbers you write a period after the number, and a zero (if it's a whole number).
对于浮点数,您在数字后写一个句点,然后写一个零(如果它是一个整数)。
Like this:
像这样:
1.0 <---- Floating point.
1.0 <---- 浮点数。
1 <------- integer
1 <------- 整数
That is how python interprets them.
这就是python解释它们的方式。
if my_answer != your_question:
print "I did not understand your question. Please rephrase it."
else:
print "Good luck. Python is fun."
I agree with rohit, the 0 is not needed. Although it makes things easier for beginners.
我同意rohit,不需要 0 。虽然它使初学者更容易。
回答by Ignacio Vazquez-Abrams
Passing a value to the float()constructor will make it a floatif possible.
如果可能,float()将值传递给构造函数将使其成为一个float。
print float(2)
print float('4.5')
Use string interpolation or formatting to display them.
使用字符串插值或格式来显示它们。
print '%.3f' % 4.53
回答by user225312
3 is an integer.
3 是一个整数。
3.0 is a float.
3.0 是一个浮点数。
>>> type(3)
<type 'int'>
>>> type(3.0)
<type 'float'>
round():
圆形的():
Round a number to a given precision in decimal digits (default 0 digits). This always returns a floating point number.
将数字舍入到给定精度的十进制数字(默认为 0 位)。这总是返回一个浮点数。
So that is why in your case, 6.0 is returned.
所以这就是为什么在您的情况下返回 6.0 的原因。
回答by Rohit
Agree with all of the answers above. You don't even need to put a zero after the period though.
同意以上所有答案。不过,您甚至不需要在句号后加零。
For example:
例如:
In [1]: type(3)
Out[1]: <type 'int'>
In [2]: type(3.)
Out[2]: <type 'float'>
回答by abe
Orange, read the last question in the lesson, the author gave you the answer
橙子,看课文最后一道题,作者给你答案
Why does / (divide) round down? It's not really rounding down; it's just dropping the fractional part after the decimal. Try doing 7.0 / 4.0 and compare it to 7 / 4 and you'll see the difference.
为什么 /(除)四舍五入?这并不是真正的四舍五入;它只是删除小数点后的小数部分。尝试执行 7.0 / 4.0 并将其与 7 / 4 进行比较,您会看到不同之处。
good luck
祝你好运
回答by Aniruddha Rairikar
Any number with a decimal is a floating point number.
任何带小数的数都是浮点数。
In python take the following example,
在python中以下面的例子为例,
If you divide 11/4 the output will be 2 [here 11 is not a floating point number]
如果除以 11/4,则输出将为 2 [此处 11 不是浮点数]
But if you divide 11.0/4, the output will be 2.75 [here 11.0 is a floating point number].
但是如果你除以 11.0/4,输出将是 2.75 [这里 11.0 是一个浮点数]。
回答by Channing Grayned
python console print(3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6) I get 7 but in pycharm I get 6.75.
python console print(3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6) 我得到 7 但在 pycharm 中我得到 6.75。

