Last active
September 21, 2024 23:17
-
-
Save deeTEEcee/d4adc0f2a2fd9e61b0bb to your computer and use it in GitHub Desktop.
Ruby custom functions that could be useful
This file contains hidden or 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
### For when you want to round up/down in custom increments of any number x (rather than just the simple case of decimal points rounding up when we reach 0.5) | |
# round_up(5, 20) -> 20 | |
# round_up(35,20) -> 40 | |
def round_up(num, round_by) | |
(num / round_by.to_f).ceil * round_by | |
end | |
# round_down(5, 20) -> 0 | |
# round_down(35, 20) -> 20 | |
def round_down(num, round_by) | |
(num / round_by) * round_by | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment