在Python中舍入到5(或其他数字)
是否可以像以下内容一样围绕内置功能?
10 -> 10
12 -> 10
13 -> 15
14 -> 15
16 -> 15
18 -> 20
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
是否可以像以下内容一样围绕内置功能?
10 -> 10
12 -> 10
13 -> 15
14 -> 15
16 -> 15
18 -> 20
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(23)
对于舍入到非全能值的舍入,例如0.05:
我发现这很有用,因为我可以在代码中进行搜索并替换以更改“ round”(“ myround”(“”而不必更改参数值。
For rounding to non-integer values, such as 0.05:
I found this useful since I could just do a search and replace in my code to change "round(" to "myround(", without having to change the parameter values.
这只是扩展的问题
It's just a matter of scaling
删除“静止”将有效:
如果值是acthy a acte a integer:
作为一个函数:
Removing the 'rest' would work:
If the value is aready an integer:
As a function:
给出
base = 5
:for
base = 10
:在Python> = 3.7.9中测试
which gives
for
base=5
:for
base=10
:tested in Python >= 3.7.9
圆形(x [,n]):值将圆形到最接近10的倍数n。因此,如果n为负...
由于10 = 5 * 2,则可以使用2个整数除法和乘法,而不是float Disemant和5.0乘法。这并不重要,除非您喜欢一点变化
round(x[, n]): values are rounded to the closest multiple of 10 to the power minus n. So if n is negative...
Since 10 = 5 * 2, you can use integer division and multiplication with 2, rather than float division and multiplication with 5.0. Not that that matters much, unless you like bit shifting
抱歉,我想对Alok Singhai的答案发表评论,但是由于缺乏声誉=/
无论如何,我们可以再概括一个步骤并走了:
这允许我们使用非企业基础,例如
.25
或任何其他分数碱基。Sorry, I wanted to comment on Alok Singhai's answer, but it won't let me due to a lack of reputation =/
Anyway, we can generalize one more step and go:
This allows us to use non-integer bases, like
.25
or any other fractional base.使用:
它不使用乘法,也不会从/转换为浮子。
四舍五入到最接近10的倍数:
如您所见,它适用于负数和正数。纽带(例如-15和15)将始终向上舍入。
一个类似的示例,该示例与5个最接近的倍数相似,表明它在不同的“基础”中的行为也如所期望的:
Use:
It does not use multiplication and will not convert from/to floats.
Rounding to the nearest multiple of 10:
As you can see, it works for both negative and positive numbers. Ties (e.g. -15 and 15) will always be rounded upwards.
A similar example that rounds to the nearest multiple of 5, demonstrating that it also behaves as expected for a different "base":
Divround的修改版:-)
Modified version of divround :-)
另一种做到这一点的方法(没有明确的乘法或划分运算符):
Another way to do this (without explicit multiplication or division operators):
如果有人需要“财务圆形”(总是0.5回合):
按文档圆形选项为:
round_ceiling
(to infinity )round_down
(to Zero)round> round_floor
(to -Infinity )round_half_down
(到最接近零的关系)round_half_even
(到最接近的纽带,均为最接近整数)round_half_up
(到距离零的领带最接近)round_up
(远离零)round_05up
是0或5;默认情况下,python
In case someone needs "financial rounding" (0.5 rounds always up):
As per documentation the rounding options are:
ROUND_CEILING
(towards Infinity)ROUND_DOWN
(towards zero)ROUND_FLOOR
(towards -Infinity)ROUND_HALF_DOWN
(to nearest with ties going towards zero)ROUND_HALF_EVEN
(to nearest with ties going to nearest even integer)ROUND_HALF_UP
(to nearest with ties going away from zero)ROUND_UP
(away from zero)ROUND_05UP
(away from zero if last digit after rounding towards zero would have been 0 or 5; otherwise towards zero)By default Python uses
ROUND_HALF_EVEN
as it has some statistical advantages (the rounded results are not biased).对于整数和Python 3:
生产:
For integers and with Python 3:
Producing:
我想没有人真正写过这篇文章,但是您可以做:
No one actually wrote this yet I guess but you can do:
5 的下一个倍数
考虑51需要转换为55:
Next multiple of 5
Consider 51 needs to be converted to 55:
那呢:
What about this:
我需要将前5个。
示例16回合降至15或19发,降至15,
这是所使用的代码
I needed to round down to the preceding 5.
Example 16 rounds down to 15 or 19 rounds down to 15
Here's the code used
这是我的C代码。如果我正确理解它,那应该是这样的东西。
而且,这也圆满到5个中的最接近的倍数,而不仅仅是四舍五入。
Here is my C code. If I understand it correctly, it should supposed to be something like this;
and this also rounds to nearest multiple of 5 instead of just rounding up;
接受的答案的补充,以指定舍入或下到最接近的5左右
An addition to accepted answer, to specify rounding up or down to nearest 5-or-whatever
一种仅适用于
int
s的解决方案(它接受float
s,但是圆形的行为就像小数组件不存在),但是与任何依赖临时的解决方案不同转换为float
(所有Math.floor
/Math.ceil
基于基于的解决方案,使用/
的所有解决方案,大多数使用rough
)的解决方案,它适用于任意巨大的int
输入,永不失去精度,永不提高异常或导致无穷大值。这是最简单的解决方案 down 到一个数字的下一个次数:
它基于的圆形配方仅仅是:
唯一的更改是,您提前将基础添加到数字中,因此它圆形到最接近。使用确切的中点值,只有使用
base
s,因此,round_to_nearest(3,6)
才能围绕6
而不是<代码> 0 ,而round_to_nearest(-3,6)
将得到0
而不是-6
。如果您更喜欢中点值,则可以将第一行更改为num +=(base -1)// 2
。A solution that works only with
int
s (it acceptsfloat
s, but the rounding behaves as if the decimal component doesn't exist), but unlike any solution relying on temporary conversion tofloat
(all themath.floor
/math.ceil
-based solutions, all the solutions using/
, most solutions usinground
), it works for arbitrarily hugeint
inputs, never losing precision, never raising exceptions or resulting in infinity values.It's an adaptation of the simplest solution for rounding down to the next lower multiple of a number:
The round down recipe it's based on is just:
the only change is that you add half the base to the number ahead of time so it rounds to nearest. With exact midpoint values, only possible with even
base
s, rounding up, soround_to_nearest(3, 6)
will round to6
rather than0
, whileround_to_nearest(-3, 6)
will round to0
rather than-6
. If you prefer midpoint values round down, you can change the first line tonum += (base - 1) // 2
.要设置5%的公差,请通过rel_tol = 0.05。默认公差为1E-09
To set a tolerance of 5%, pass rel_tol=0.05. The default tolerance is 1e-09
我发现这比 @mkrieger1和@alok singhal的答案要慢得多,但它更明确地说明了圆形行为,并且更易于修改或扩展。
I find this one to be negligibly slower than the answer by @mkrieger1 and @Alok Singhal but it is more explicit about the rounding behavior and easier to modify or extend.
您可以“技巧”
int()
进行四舍五入您传递给
int()
的号码。You can “trick”
int()
into rounding off instead of rounding down by adding0.5
to thenumber you pass to
int()
.我不知道Python中的标准功能,但这对我来说有用:
Python 3
很容易理解上述原因。您要确保您的电话号码除以5是一个整数,正确圆形。因此,我们首先做到这一点(
回合(x/5)
),然后自从我们除以5,我们也乘以5。我通过给出
base
参数,默认为5。python2
在Python 2中,
float(x)
将需要使用 python 2,从而 使该功能更加通用。 /确实需要浮点线,并且需要最终转换为int
,因为round()
返回Python 2中的浮点值。I don't know of a standard function in Python, but this works for me:
Python 3
It is easy to see why the above works. You want to make sure that your number divided by 5 is an integer, correctly rounded. So, we first do exactly that (
round(x/5)
), and then since we divided by 5, we multiply by 5 as well.I made the function more generic by giving it a
base
parameter, defaulting to 5.Python 2
In Python 2,
float(x)
would be needed to ensure that/
does floating-point division, and a final conversion toint
is needed becauseround()
returns a floating-point value in Python 2.