2016年11月15日 / 370次阅读
Python
Python中的round()是一个很有用的built-in函数,可以将float直接转化成int,也可以控制float数值的小数位数。
>>>
>>> help(round)
Help on built-in function round in module builtins:
round(...)
round(number[, ndigits]) -> number
Round a number to a given precision in decimal digits (default 0 digits).
This returns an int when called with one argument, otherwise the
same type as the number. ndigits may be negative.
>>>
>>>
>>> round(11.12345)
11
>>> round(11.12345,3)
11.123
>>> round(11.12345,-3)
0.0
>>> round(11.12345,-1)
10.0
>>> round(11.12345,0)
11.0
>>>
>>>
这些小巧玲珑的built-in函数,可以让我们的编程快很多。
>>> 8/3 # 除法,返回的永远是float
2.6666666666666665
>>> 8//3 # 除数
2
>>> 8%3 # 余数
2
>>> round(8/3) # round函数要做四舍五入,这里返回的是int
3
>>> round(8/3, 2) # round函数要做四舍五入,这里返回的是float
2.67
>>>
本文链接:https://www.maixj.net/ict/python-round-13626
©Copyright 麦新杰 Since 2014 云上小悟独立博客版权所有 备案号:苏ICP备14045477号-1。云上小悟网站部分内容来源于网络,转载目的是为了整合信息,收藏学习,服务大家,有些转载内容也难以判断是否有侵权问题,如果侵犯了您的权益,请及时联系站长,我会立即删除。