Skip to content

Instantly share code, notes, and snippets.

@bensbigolbeard
Created February 4, 2014 17:00
Show Gist options
  • Save bensbigolbeard/8807812 to your computer and use it in GitHub Desktop.
Save bensbigolbeard/8807812 to your computer and use it in GitHub Desktop.
def convert(digit)
result = ""
remainder = digit
while remainder > 0
case remainder
when remainder <= 1000
result << "M"
remainder = remainder - 1000
when 900..999
result << "CM"
remainder = remainder % 900
when 500..899
result << "D"
remainder = remainder % 500
when 400..499
result << "CD"
remainder = remainder % 400
when 100..399
result << "C"
remainder = remainder - 100
when 90..100
result << "XC"
remainder = remainder % 90
when 50..89
result << "L"
remainder = remainder - 50
when 40..49
result << "XL"
remainder = remainder % 40
when 10..39
result << "X"
remainder = remainder - 10
when 9
result << "IX"
remainder -= 9
when 5..8
result << "V"
remainder = remainder % 5
when 4
result << "IV"
remainder -= 4
when 1..3
result << "I"
remainder = remainder - 1
end
end
result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment