Created
September 26, 2013 21:52
-
-
Save adayag/6721116 to your computer and use it in GitHub Desktop.
Method to convert numbers to their roman numeral value
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
| 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