[英]Rounding a Whole no. to nearest number in python
Hello!你好!
I am designing a project in python where I'm stuck at a point.我正在 python 设计一个项目,我被困在了一个点上。 I hope I'll get my solution.
我希望我能得到我的解决方案。
Python: 3.7.9 on windows 10 Python:3.7.9 在 windows 10
ie for example:即例如:
I cannot comment because I don't have sufficient reputation.我无法发表评论,因为我没有足够的声誉。 But you can refere to this link here:
但是你可以在这里参考这个链接:
Round to 5 (or other number) in Python 在 Python 中四舍五入到 5(或其他数字)
A slight tweak can lead you to the right way:稍微调整一下就可以引导您走上正确的道路:
def myround(x, base=10):
return base * round(x/base)
print(myround(1))
You can use the round()
function like this.您可以像这样使用
round()
function。
print(round(46,-1))
print(round(45,-1))
print(round(715306,-1))
50
40
715310
When we round values, we go from a numerical value with decimal places to a whole number.当我们对值进行四舍五入时,我们将 go 从带小数位的数值变成整数。 With this process we do lose some precision, but the rounded value is often much easier to read and interpret.
通过这个过程,我们确实失去了一些精度,但四舍五入的值通常更容易阅读和解释。
Python has three ways to turn a floating-point value into a whole (integer) number: Python 有三种方法可以将浮点值转换为整数(整数):
The built-in round() function rounds values up and down.内置的 round() function 将值上下舍入。
round(5.4598)圆形(5.4598)
The math.floor() function rounds down to the next full integer. math.floor() function 向下舍入到下一个完整的 integer。
import math导入数学
math.floor(12.75)数学.floor(12.75)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.