我如何只在Python中舍入一个数字/浮点数?
我将生成此随机数,例如12.75或1.999999999或2.65
I will have this random number generated e.g 12.75 or 1.999999999 or 2.65
我希望始终将此数字舍入为最接近的整数整数,以便2.65将舍入2。
I want to always round this number down to the nearest integer whole number so 2.65 would be rounded to 2.
很抱歉,但经过无数次搜索后我找不到答案,谢谢:)
Sorry for asking but I couldn't find the answer after numerous searches, thanks :)
您可以使用 int()
, math.trunc(),或 math.floor()。他们都会做你想要的正数:
You can us either int()
, math.trunc(), or math.floor(). They all will do what you want for positive numbers:
>>> import math
>>> math.floor(12.6) # returns 12.0 in Python 2
12
>>> int(12.6)
12
>>> math.trunc(12.6)
12
但请注意,它们的行为与负数不同:int和 math.trunc
将变为0,而 math.floor
总是向下:
However, note that they behave differently with negative numbers: int and math.trunc
will go to 0, whereas math.floor
always floors downwards:
>>> import math
>>> math.floor(-12.6) # returns -13.0 in Python 2
-13
>>> int(-12.6)
-12
>>> math.trunc(-12.6)
-12
请注意 math.floor
和 math.ceil
用于返回Python 2中的浮点数。
Note that math.floor
and math.ceil
used to return floats in Python 2.
另请注意, int
和 math.trunc
两者(乍一看)似乎都会做同样的事情,尽管他们的确切的语义不同。简而言之:int用于一般/类型转换, math.trunc
专门用于数字类型(并且有助于使您的意图更清晰)。
Also note that int
and math.trunc
will both (at first glance) appear to do the same thing, though their exact semantics differ. In short: int is for general/type conversion and math.trunc
is specifically for numeric types (and will help make your intent more clear).
使用 int
如果你真的不在乎差异,如果你想转换字符串,或者如果你不想你不想导入一个库。使用 trunc
如果您希望绝对明确您的意思,或者您希望确保您的代码能够正常运行非内置类型。
Use int
if you don't really care about the difference, if you want to convert strings, or if you don't want to import a library. Use trunc
if you want to be absolutely unambiguous about what you mean or if you want to ensure your code works correctly for non-builtin types.
以下更多信息:
Python 2中的Math.floor()与Python相比3
请注意 math.floor
(以及 math.ceil
)从Python 2略微改为Python 3 - 在Python 2中,两个函数都将返回float而不是int。在Python 3中对此进行了更改,以便两个方法都返回一个int(更具体地说,它们在给出的任何对象上调用 __ float __
方法)。那么,如果您正在使用Python 2,或者希望您的代码保持两个版本之间的兼容性,那么通常可以安全地执行 int(math.floor(...))
。
Note that math.floor
(and math.ceil
) were changed slightly from Python 2 to Python 3 -- in Python 2, both functions will return a float instead of an int. This was changed in Python 3 so that both methods return an int (more specifically, they call the __float__
method on whatever object they were given). So then, if you're using Python 2, or would like your code to maintain compatibility between the two versions, it would generally be safe to do int(math.floor(...))
.
有关进行此更改的原因的更多信息+关于执行 int(math.floor(。)的潜在缺陷。 ..))
在Python 2中,请参阅
为什么Python的math.ceil()和数学.floor()操作返回浮点数而不是整数?
For more information about why this change was made + about the potential pitfalls of doing int(math.floor(...))
in Python 2, see
Why do Python's math.ceil() and math.floor() operations return floats instead of integers?
int vs math.trunc()
乍一看, int()
和 math.trunc()
方法看起来是完全相同的。主要区别是:
At first glance, the int()
and math.trunc()
methods will appear to be identical. The primary differences are:
- int(...)
- int函数将接受浮点数,字符串和整数。
- 运行
int(param)
将调用param .__ int __()
方法以执行转换(然后尝试调用__ trunc __
if__ int __
未定义) -
__ int __
魔术方法并非始终无明确定义 - 在一段时间内,事实证明,__ int __
的确切语义和规则应该在很大程度上依赖于实现类。 - 当您想将通用对象转换为int时,可以使用
int
函数。这是一种类型转换方法。例如,您可以通过执行int(42)
将字符串转换为整数(或执行更改基数:int(AF) ,16) - > 175
)。
-
int(...)
- The int function will accept floats, strings, and ints.
- Running
int(param)
will call theparam.__int__()
method in order to perform the conversion (and then will try calling__trunc__
if__int__
is undefined) - The
__int__
magic method was not always unambiguously defined -- for some period of time, it turned out that the exact semantics and rules of how__int__
should work were largely left up to the implementing class. - The
int
function is meant to be used when you want to convert a general object into an int. It's a type conversion method. For example, you can convert strings to ints by doingint("42")
(or do things like change of base:int("AF", 16) -> 175
).
- trunc只接受数字类型(整数,浮点数等)
- 正在运行
math.trunc(param)
函数将调用param .__ trunc __()
方法以执行转换 - __ trunc __ 魔术方法的确切行为和语义/ peps / pep-3141 /rel =nofollow noreferrer> PEP 3141 (更具体地说,在对操作和__magic__方法的更改部分)。
-
c> math.trunc 函数用于获取现有实数,特别是 truncate和删除其小数以生成整数类型。这意味着与 int
不同,math.trunc
是一个纯数字运算。
- The trunc will only accept numeric types (ints, floats, etc)
- Running
math.trunc(param)
function will call theparam.__trunc__()
method in order to perform the conversion - The exact behavior and semantics of the
__trunc__
magic method was precisely defined in PEP 3141 (and more specifically in the Changes to operations and __magic__ methods section). - The
math.trunc
function is meant to be used when you want to take an existing real number and specifically truncate and remove its decimals to produce an integral type. This means that unlikeint
,math.trunc
is a purely numeric operation.
所有这一切都表明,无论你使用int还是int,所有Python的内置类型都会表现完全相同TRUNC。这意味着,如果你所做的只是使用常规的整数,浮动,分数,并小数,您可以自由使用int或trunc。
All that said, it turns out all of Python's built-in types will behave exactly the same whether you use int or trunc. This means that if all you're doing is using regular ints, floats, fractions, and decimals, you're free to use either int or trunc.
但是,如果你想要非常准确地确定你的意图是什么(例如,如果你想让它绝对清楚你是否是地板或截断),或者如果您正在使用具有
__ int __
和__ trunc __
的不同实现的自定义数字类型,那么它可能是最好的使用math.trunc
。However, if you want to be very precise about what exactly your intent is (ie if you want to make it absolutely clear whether you're flooring or truncating), or if you're working with custom numeric types that have different implementations for
__int__
and__trunc__
, then it would probably be best to usemath.trunc
.您还可以在 Python的开发人员邮件列表。