Created
October 25, 2013 20:58
-
-
Save danielnc/7161732 to your computer and use it in GitHub Desktop.
Stupid and un-optimized converter to other numerical bases(NUMBER.to_s(base))
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 convert(number, base=10) | |
raise "base must be between 2 and 36" if base < 2 || base > 36 | |
negative = number < 0 | |
number = number.abs | |
number_array = [] | |
loop do | |
number_array.unshift("0123456789abcdefghijklmnopqrstuvwxyz"[number % base]) | |
number /= base | |
break if number == 0 | |
end | |
number_array.unshift("-") if negative | |
number_array.join | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment