How do i round an integer to the nearest multiple of ten?
I have tried integer.round(0.1)
but it gives the nearest decimal number.
Example: 3 should return 0, 55 should return 60.
Is there a method that will round to the nearest multiple of 10?
Integer#round has the functionality.
You pass a negative number to round
to represent which 10's digit you'd want to round to. For example:
Round to the nearest 10:
55.round(-1) # => 60
To round to the nearest 100:
550.round(-2) # => 600
You can just divide by 10, round, then multiply by 10:
nearest = (x/ 10).round * 10
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With