开发者

Python: How to round 123 to 100 instead of 100.0?

>>> round(123,-2)
100.0
>>> 

How to round it to 100 instead of 100开发者_运维问答.0? 


int(round(123,-2))

The int function can be used to convert a string or number to a plain integer.


you can just throw it in int:

In [1]: int(round(123, -2))
Out[1]: 100


You could use int(100.0) to convert to 100 in python 2.x and in python3.x, its just works

Python 3.1.2 (r312:79149, ...
>>>
>>> round(123,-2)
100
>>>


You can use regular expression : (\d+)\..* to match the entire thing and extract only the integer part.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜