Created
June 17, 2014 14:25
-
-
Save theinventor/654a3902f100fb7d52db to your computer and use it in GitHub Desktop.
Round to nearest 5 minutes in ruby, strangely this wasn't super simple to come up with..
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def self.round_to_five_minutes(minute) | |
all = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55] | |
higher = nil | |
lower = nil | |
all.each_with_index do |five_min,index| | |
if minute == five_min | |
return five_min | |
end | |
if five_min > minute | |
higher = five_min | |
lower = all[index-1] | |
break | |
end | |
end | |
diff_lower = minute-lower | |
diff_higher = higher-minute | |
diff_lower > diff_higher ? higher : lower | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment