I want to round numbers up to their nearest order of magnitude. (I think I said this right)
Here are some examples:
Input => Output
8 => 10
34 => 40
99 => 100
120 => 200
360 => 400
990 => 1000
1040 => 2000
1620 => 2000
5070 => 6000
9000 => 10000
Anyone know a quick way to write that in Ruby or Rails?
Essentially I need to know the order of magnitude of the number and how to round by that precision.
Thanks!
Here's another way:
def roundup(num)
x = Math.log10(num).floor
num=(num/(10.0**x)).ceil*10**x
return num
end
More idiomatically:
def roundup(num)
x = Math.log10(num).floor
(num/(10.0**x)).ceil * 10**x
end
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