English 中文(简体)
Python to
原标题:Python round up integer to next hundred

本应被问到数百次(pun=fun=)的种子,但只能为圆浮体找到功能。 我如何打整一种分类,例如:130 -> 200?

最佳回答

圆环通常是在浮动点数上进行的,在这里,你应当知道三项基本功能:round。 (四舍五入) http://docs.python.org/library/math.html#math.ceil”rel=“noreferer”>math.ceil。 (长途)

你们问到的是愤怒和四舍五入到数百,但只要你的人数少于2<>>53,我们仍可使用<条码>。 为了使用<条码>数学.ceil,我们刚刚将头100分,四舍五入,后100分乘:

>>> import math
>>> def roundup(x):
...     return int(math.ceil(x / 100.0)) * 100
... 
>>> roundup(100)
100
>>> roundup(101)
200

右边两处“临时”两处100处,先拨100个,然后乘以100个。 如果希望四舍五入到十条(n = 1),则可以使用10**n而不是100(n = 3)。

An alternative way to do this is to avoid floating point numbers (they have limited precision) and instead use integers only. Integers have arbitrary precision in Python, so this lets you round numbers of any size. The rule for rounding is simple: find the remainder after division with 100, and add 100 minus this remainder if it s non-zero:

>>> def roundup(x):
...     return x if x % 100 == 0 else x + 100 - x % 100

这涉及任何规模的数量:

>>> roundup(100)
100
>>> roundup(130)
200
>>> roundup(1234567891234567891)
1234567891234567900L

我采用了两种解决办法的微型基准:

$ python -m timeit -s  import math  -s  x = 130   int(math.ceil(x/100.0)) * 100 
1000000 loops, best of 3: 0.364 usec per loop
$ python -m timeit -s  x = 130   x if x % 100 == 0 else x + 100 - x % 100 
10000000 loops, best of 3: 0.162 usec per loop

与<代码>数学.ceil解决方案相比,纯粹的分类账解决办法要快两倍。

Thomas建议采用一种与我上述办法相同的基于愤怒的解决办法,但采用乘以诱骗人的价值观。 令人感兴趣的是,这样写法的速度没有好处:

$ python -m timeit -s  x = 130   x + 100*(x%100>0) - x%100 
10000000 loops, best of 3: 0.167 usec per loop

最后,我要指出,如果你想要打第101-149至100轮,而第150-199至200轮,例如,四舍五入到nearest100,那么就在上架设了<>环形>。 你们可以发挥这一作用:

>>> int(round(130, -2))
100
>>> int(round(170, -2))
200
问题回答

这是一个过时的答案,但有一个简单的解决办法,将现有答复的最佳方面结合起来:从<条码>x>>到<条码>100/代码>的下一个多处是<条码>x%-100。 (或者如果你愿意的话,x + (-x) % 100

>>> x = 130
>>> x -= x % -100  # Round x up to next multiple of 100.
>>> x
200

这既迅速又简单,为任何星号x(如John Machin的回答)提供正确结果,并且如果<>x<>>x>> 代码(如Martin Geisler的回答)为浮动,则得出合理的结果(关于浮动点代表的通常警告)。

>>> x = 0.1
>>> x -= x % -100
>>> x
100.0

为此:

int(round(130 + 49, -2))

在这里,总的方法是,四舍五入到最接近的多种积极愤怒:

def roundUpToMultiple(number, multiple):
    num = number + (multiple - 1)
    return num - (num % multiple)

样本使用:

>>> roundUpToMultiple(101, 100)
200
>>> roundUpToMultiple(654, 321)
963

http://www.un.org/Depts/DGACM/index_french.htm

>>> rup = lambda a, b: (a + b - 1) // b * b
>>> [(x, rup(x, 100)) for x in (199, 200, 201)]
[(199, 200), (200, 200), (201, 300)]

<>Update>UpdateThe present-accepted responsefall di with integers such that sign(x) /pot(y) can t is t be reflected as a float. 见本法典:

import math

def geisler(x, y): return int(math.ceil(x / float(y))) * y

def orozco(x, y): return x + y * (x % y > 0) - x % y

def machin(x, y): return (x + y - 1) // y * y

for m, n in (
    (123456789123456789, 100),
    (1234567891234567891, 100),
    (12345678912345678912, 100),
    ):
    print; print m, "m"; print n, "n"
    for func in (geissler, orozco, machin):
        print func(m, n), func.__name__

Output:

123456789123456789 m
100 n
123456789123456800 geisler
123456789123456800 orozco
123456789123456800 machin

1234567891234567891 m
100 n
1234567891234568000 geisler <<<=== wrong
1234567891234567900 orozco
1234567891234567900 machin

12345678912345678912 m
100 n
12345678912345680000 geisler <<<=== wrong
12345678912345679000 orozco
12345678912345679000 machin

这里是一些时机:

>python27python -m timeit -s "import math;x =130" "int(math.ceil(x/100.0))*100"
1000000 loops, best of 3: 0.342 usec per loop

>python27python -m timeit -s "x = 130" "x + 100 * (x % 100 > 0) - x % 100"
10000000 loops, best of 3: 0.151 usec per loop

>python27python -m timeit -s "x = 100" "(x + 99) // 100 * 100"
10000000 loops, best of 3: 0.0903 usec per loop

Try this:

import math

def ceilm(number, multiple):
       Returns a float rounded up by a factor of the multiple specified   
    return math.ceil(float(number)/multiple) * multiple

样本使用:

>>> ceilm(257, 5)
260
>>> ceilm(260, 5)
260

如果您的身为x:x + 100 - x % 100

然而,正如评论中指出的,如果<代码>x=100,将回去200。

如果这不是预期行为,请使用<代码>x + 100*(x%100>0)-x%<100/code>。

警告:今后的成熟程度......

Since so many of the answers here do the timing of this I wanted to add another alternative.

Taking @Martin Geisler s

def roundup(x):
    return x if x % 100 == 0 else x + 100 - x % 100

(which i like best for several reasons)

但不计入行动百分比

def roundup2(x):
    x100= x % 100
    return x if x100 == 0 else x + 100 - x100

Yi 速度比原机提高20%

def roundup3(x):
    x100 = x % 100
    return x if not x100 else x + 100 - x100

甚至更好,比原来更快约36%

最后,我认为,我可以放弃<代码>而不是的操作者,并改变各分支的次序,希望这也能够加快速度,但是为了发现实际上退缩速度缓慢,只有23%比原更快。

def roundup4(x):
    x100 = x % 100
    return x + 100 - x100  if x100 else x


>python -m timeit -s "x = 130" "x if x % 100 == 0 else x + 100 - x % 100"
1000000 loops, best of 3: 0.359 usec per loop

>python -m timeit -s "x = 130" "x100 = x % 100"  "x if x100 == 0 else x + 100 - x100"
1000000 loops, best of 3: 0.287 usec per loop

>python -m timeit -s "x = 130" "x100 = x % 100"  "x if not x100 else x + 100 - x100"
1000000 loops, best of 3: 0.23 usec per loop

>python -m timeit -s "x = 130" "x100 = x % 100"  "x + 100 - x100 if x100 else x"
1000000 loops, best of 3: 0.277 usec per loop

最好解释为什么要加快3,然后是4。

这是一个非常简单的解决办法:

next_hundred = x//100*100+100

www.un.org/Depts/DGACM/index_spanish.htm 如何工作?

  1. Perform the integer division by 100 (it basically cuts off the fractional part of the normal division). In this way you obtain the tens of a number. For example: 243//100=2.
  2. Multiply by 100, getting the original number without its tens and ones. For example: 2*100=200.
  3. Add 100 to get the desired result. For example: 200+100=300

www.un.org/Depts/DGACM/index_spanish.htm 一些实例。

  • 0...99 rounded to 100
  • 100...199 rounded to 200
  • etc.

稍加修改的办法第1.100至100、10.200至200轮等:

next_hundred = (x-1)//100*100+100

简单:

round(599, -2)

will give:

600





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签