Advertisement
Guest User

IntToRoman

a guest
Oct 19th, 2016
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.68 KB | None | 0 0
  1. local function intoroman(n)
  2.     if n < 1  or n > 3999 then
  3.         return ""
  4.     end
  5.     local numer_toconvert = n
  6.     local number_roman = ""
  7.     local table_conversion = {
  8.                         ['1'] = {"","I","II","III","IV","V","VI","VII","VIII","IX"},
  9.                         ['10'] = {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"},
  10.                         ['100'] = {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"},
  11.                         ['1000'] = {"","M","MM","MMM"}
  12.     }
  13.     for x =3,0, -1 do
  14.         local index_n = math.pow(10, x)
  15.         local temp_r = math.floor(numer_toconvert / index_n)
  16.         number_roman = number_roman .. table_conversion["" .. index_n][temp_r+1]
  17.         numer_toconvert = numer_toconvert -  (temp_r* index_n)
  18.     end
  19.     return number_roman
  20. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement