在Python中舍入到5(或其他数字)

时间:2010-02-16 10:35:04

标签: python rounding

是否有内置函数可以像下面这样循环?

10 -> 10
12 -> 10
13 -> 15
14 -> 15
16 -> 15
18 -> 20

18 个答案:

答案 0 :(得分:246)

我不知道Python中的标准函数,但这对我有用:

Python 2

def myround(x, base=5):
    return int(base * round(float(x)/base))

Python3

def myround(x, base=5):
    return base * round(x/base)

很容易理解上述原因。你想确保你的数字除以5是一个整数,正确舍入。所以,我们首先这样做(round(float(x)/5)只在Python2中需要float),然后由于我们除以5,我们也乘以5。最终转换为int是因为round()在Python 2中返回浮点值。

我通过给它一个base参数使该函数更通用,默认为5。

答案 1 :(得分:38)

用于舍入为非整数值,例如0.05:

def myround(x, prec=2, base=.05):
  return round(base * round(float(x)/base),prec)

我发现这很有用,因为我可以在我的代码中进行搜索和替换,将“round(”更改为“myround(”,而不必更改参数值。

答案 2 :(得分:19)

这只是缩放问题

>>> a=[10,11,12,13,14,15,16,17,18,19,20]
>>> for b in a:
...     int(round(b/5.0)*5.0)
... 
10
10
10
15
15
15
15
15
20
20
20

答案 3 :(得分:11)

删除'休息'会工作:

rounded = int(val) - int(val) % 5

如果值为isady是整数:

rounded = val - val % 5

作为一项功能:

def roundint(value, base=5):
    return int(value) - int(value) % int(base)

答案 4 :(得分:7)

def round_to_next5(n):
    return n + (5 - n) % 5

答案 5 :(得分:6)

round(x [,n]):将值四舍五入为函数减去n的最接近的10的倍数。所以,如果n是否定的......

def round5(x):
    return int(round(x*2, -1)) / 2

由于10 = 5 * 2,你可以使用整数除法和乘法2,而不是浮点除法和乘以5.0。不是那么重要,除非你喜欢比特移动

def round5(x):
    return int(round(x << 1, -1)) >> 1

答案 6 :(得分:4)

很抱歉,我想对Alok Singhai的答案发表评论,但由于缺乏声誉,它不会让我失望= /

无论如何,我们可以再推广一步并去:

def myround(x, base=5):
    return base * round(float(x) / base)

这允许我们使用非整数碱基,如.25或任何其他分数碱基。

答案 7 :(得分:3)

def round_up_to_base(x, base=10):
    return x + (base - x) % base

def round_down_to_base(x, base=10):
    return x - (x % base)

给出

对于base=5

>>> [i for i in range(20)]
[0, 1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> [round_down_to_base(x=i, base=5) for i in range(20)]
[0, 0,  0,  0,  0,  5,  5,  5,  5,  5,  10, 10, 10, 10, 10, 15, 15, 15, 15, 15]

>>> [round_up_to_base(x=i, base=5) for i in range(20)]
[0, 5,  5,  5,  5,  5,  10, 10, 10, 10, 10, 15, 15, 15, 15, 15, 20, 20, 20, 20]

对于base=10

>>> [i for i in range(20)]
[0, 1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> [round_down_to_base(x=i, base=10) for i in range(20)]
[0, 0,  0,  0,  0,  0,  0,  0,  0,  0,  10, 10, 10, 10, 10, 10, 10, 10, 10, 10]

>>> [round_up_to_base(x=i, base=10) for i in range(20)]
[0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 20, 20, 20, 20]

在 Python 3.7.9 中测试

答案 8 :(得分:3)

divround的修改版本: - )

def divround(value, step, barrage):
    result, rest = divmod(value, step)
    return result*step if rest < barrage else (result+1)*step

答案 9 :(得分:2)

使用:

>>> from __future__ import division   # This is only needed on Python 2
>>> def round_to_nearest(n, m):
        r = n % m
        return n + m - r if r + r >= m else n - r

...

它不使用乘法,也不会从/转换为浮点数。

舍入到最接近的10的倍数:

>>> for n in range(-21, 30, 3): print('{:3d}  =>  {:3d}'.format(n, round_to_nearest(n, 10)))
-21  =>  -20
-18  =>  -20
-15  =>  -10
-12  =>  -10
 -9  =>  -10
 -6  =>  -10
 -3  =>    0
  0  =>    0
  3  =>    0
  6  =>   10
  9  =>   10
 12  =>   10
 15  =>   20
 18  =>   20
 21  =>   20
 24  =>   20
 27  =>   30

正如您所看到的,它适用于负数和正数。领带(例如-15和15)将始终向上舍入。

一个类似的例子,它舍入到最接近的5的倍数,表明它的行为也符合预期的不同基数&#34;:

>>> for n in range(-21, 30, 3): print('{:3d}  =>  {:3d}'.format(n, round_to_nearest(n, 5)))
-21  =>  -20
-18  =>  -20
-15  =>  -15
-12  =>  -10
 -9  =>  -10
 -6  =>   -5
 -3  =>   -5
  0  =>    0
  3  =>    5
  6  =>    5
  9  =>   10
 12  =>   10
 15  =>   15
 18  =>   20
 21  =>   20
 24  =>   25
 27  =>   25

答案 10 :(得分:1)

如果有人需要“金融四舍五入”(0.5轮总是上涨):

def myround(x, base=5):
    roundcontext = decimal.Context(rounding=decimal.ROUND_HALF_UP)
    decimal.setcontext(roundcontext)
    return int(base *float(decimal.Decimal(x/base).quantize(decimal.Decimal('0'))))

根据文档,其他舍入选项包括:

  

ROUND_CEILING(朝向无限),
  ROUND_DOWN(朝零),
  ROUND_FLOOR(朝向-Infinity),
  ROUND_HALF_DOWN(最接近关系为零),
  ROUND_HALF_EVEN(最近的关系为最接近的偶数),
  ROUND_HALF_UP(距离零最近的关系),或者   ROUND_UP(远离零)。
  ROUND_05UP(如果在向零舍入后的最后一位数字为0或5,则为零;否则为零)

默认情况下,Python使用ROUND_HALF_EVEN,因为它具有一些统计优势(舍入结果没有偏差)。

答案 11 :(得分:1)

对于整数和Python 3:

def divround_down(value, step):
    return value//step*step


def divround_up(value, step):
    return (value+step-1)//step*step

制作:

>>> [divround_down(x,5) for x in range(20)]
[0, 0, 0, 0, 0, 5, 5, 5, 5, 5, 10, 10, 10, 10, 10, 15, 15, 15, 15, 15]
>>> [divround_up(x,5) for x in range(20)]
[0, 5, 5, 5, 5, 5, 10, 10, 10, 10, 10, 15, 15, 15, 15, 15, 20, 20, 20, 20]

答案 12 :(得分:1)

执行此操作的另一种方法(无显式乘法或除法运算符):

def rnd(x, b=5):
    return round(x + min(-(x % b), b - (x % b), key=abs))

答案 13 :(得分:1)

我猜还没有人写这个,但是你可以做到:

round(12, -1) --> 10
round(18, -1) --> 20

答案 14 :(得分:0)

这是我的C代码。如果我正确理解,应该应该是这样;

#include <stdio.h>

int main(){
int number;

printf("Enter number: \n");
scanf("%d" , &number);

if(number%5 == 0)
    printf("It is multiple of 5\n");
else{
    while(number%5 != 0)
        number++;
  printf("%d\n",number);
  }
}

,它也四舍五入到最接近的5的倍数,而不仅仅是四舍五入;

#include <stdio.h>

int main(){
int number;

printf("Enter number: \n");
scanf("%d" , &number);

if(number%5 == 0)
    printf("It is multiple of 5\n");
else{
    while(number%5 != 0)
        if (number%5 < 3)
            number--;
        else
        number++;
  printf("nearest multiple of 5 is: %d\n",number);
  }
}

答案 15 :(得分:0)

这个怎么样:

 def divround(value, step):
     return divmod(value, step)[0] * step

答案 16 :(得分:-2)

下一个5的多个

考虑51需要转换为55:

code here

mark = 51;
r = 100 - mark;
a = r%5;
new_mark = mark + a;

答案 17 :(得分:-3)

您可以通过向int()添加0.5来“欺骗”int()进行四舍五入而不是向下舍入 您传递给{{1}}的号码。

相关问题