-1

Rounding a Whole no. to nearest number in python

Hello!
I am designing a project in python where I'm stuck at a point. I hope I'll get my solution.
Python : 3.7.9 on windows 10
i.e. for example:

  1. 0 --> 0
  2. 1 --> 0
  3. 2 --> 0
  4. 3 --> 0
  5. 4 --> 0
  6. 5 --> 0
  7. 6 --> 10
  8. 7 --> 10
  9. 8 --> 10
  10. 9 --> 10
  11. 10 --> 10
  12. 715306 --> 715310
  13. 715305 --> 715300 similarly for all big no.s too Thank you!

3 Answers3

1

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

A slight tweak can lead you to the right way:

def myround(x, base=10):
    return base * round(x/base)
print(myround(1))
0

You can use the round() function like this.

print(round(46,-1))
print(round(45,-1))
print(round(715306,-1))
50
40
715310
Ram
  • 4,724
  • 2
  • 14
  • 22
0

Round numerical values up and down in Python

When we round values, we go from a numerical value with decimal places to a whole number. 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:

  • The built-in round() function rounds values up and down.

    round(5.4598)

  • The math.floor() function rounds down to the next full integer.

    import math

    math.floor(12.75)

RITIK KUMAR
  • 137
  • 1
  • 7