简体   繁体   English

四舍五入到 python 中最接近的数字

[英]Rounding a Whole no. to nearest number in python

Rounding a Whole no.四舍五入to nearest number in python到 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:即例如:

  1. 0 --> 0 0 --> 0
  2. 1 --> 0 1 --> 0
  3. 2 --> 0 2 --> 0
  4. 3 --> 0 3 --> 0
  5. 4 --> 0 4 --> 0
  6. 5 --> 0 5 --> 0
  7. 6 --> 10 6 --> 10
  8. 7 --> 10 7 --> 10
  9. 8 --> 10 8 --> 10
  10. 9 --> 10 9 --> 10
  11. 10 --> 10 10 --> 10
  12. 715306 --> 715310 715306 --> 715310
  13. 715305 --> 715300 similarly for all big no.s too Thank you! 715305 --> 715300 同样适用于所有大号码 谢谢!

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

Round numerical values up and down in Python Python 中上下舍入数值

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM