Skip to content

Instantly share code, notes, and snippets.

@adayag
Created September 26, 2013 21:52
Show Gist options
  • Save adayag/6721116 to your computer and use it in GitHub Desktop.
Save adayag/6721116 to your computer and use it in GitHub Desktop.
Method to convert numbers to their roman numeral value
def to_roman(num)
roman = ""
roman_numerals = [['M', 1000],
['CM', 900],
['D', 500],
['CD',400],
['C', 100],
['XC', 90],
['L', 50],
['XL', 40],
['X', 10],
['IX', 9],
['V', 5],
['IV', 4],
['I', 1]]
roman_numerals.each do |numeral|
roman << numeral[0]*(num/numeral[1])
num = num%numeral[1]
end
return roman
end
puts to_roman(1) == "I"
puts to_roman(3) == "III"
puts to_roman(6) == "VI"
puts to_roman(944)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment