Python将整数舍入到下一百 [英] Python round up integer to next hundred

查看:102
本文介绍了Python将整数舍入到下一百的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎应该已经被问过数百次了(双关语很有趣=),但我只能找到舍入浮点数的函数.如何舍入一个整数,例如:130 -> 200?

Seems that should have already been asked hundreds (pun are fun =) of times but i can only find function for rounding floats. How do I round up an integer, for example: 130 -> 200 ?

推荐答案

舍入通常在浮点数上完成,这里您应该了解三个基本功能:

Rounding is typically done on floating point numbers, and here there are three basic functions you should know: round (rounds to the nearest integer), math.floor (always rounds down), and math.ceil (always rounds up).

您询问整数并将其四舍五入,但是只要您的数字小于2 53 ,我们仍然可以使用math.ceil.要使用math.ceil,我们先将其除以100,四舍五入,然后再乘以100:

You ask about integers and rounding up to hundreds, but we can still use math.ceil as long as your numbers smaller than 253. To use math.ceil, we just divide by 100 first, round up, and multiply with 100 afterwards:

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

先除以100,然后再乘以100,然后将小数点"向左和向右移动两位,这样math.ceil就可以在数百个点上使用.如果要舍入到十进制(n = 1),千位数(n = 3)等,可以使用10**n而不是100.

Dividing by 100 first and multiply with 100 afterwards "shifts" two decimal places to the right and left so that math.ceil works on the hundreds. You could use 10**n instead of 100 if you want to round to tens (n = 1), thousands (n = 3), etc.

执行此操作的另一种方法是避免浮点数(它们的精度有限),而仅使用整数.整数在Python中具有任意精度,因此这使您可以舍入任意大小的数字.四舍五入的规则很简单:除以100后找到余数,如果非零则加100减去该余数:

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

这适用于任何大小的数字:

This works for numbers of any size:

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

我对这两种解决方案做了一个迷你基准测试:

I did a mini-benchmark of the two solutions:

$ 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

math.ceil解决方案相比,纯整数解决方案的速度快两倍.

The pure integer solution is faster by a factor of two compared to the math.ceil solution.

Thomas提出了一种基于整数的解决方案,该解决方案与我上面的解决方案完全相同,不同之处在于它通过将布尔值相乘来使用技巧.有趣的是,以这种方式编写代码没有速度优势:

Thomas proposed an integer based solution that is identical to the one I have above, except that it uses a trick by multiplying Boolean values. It is interesting to see that there is no speed advantage of writing the code this way:

$ 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,例如,舍入到最近的百位,则内置的round函数可以为您做到这一点:

As a final remark, let me also note, that if you had wanted to round 101–149 to 100 and round 150–199 to 200, e.g., round to the nearest hundred, then the built-in round function can do that for you:

>>> int(round(130, -2))
100
>>> int(round(170, -2))
200

这篇关于Python将整数舍入到下一百的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆