Roman Numerals Conversion

Sarah Reichelt sarah.reichelt at gmail.com
Thu Mar 25 19:42:43 EDT 2010


> I've searched around, and have found many
> scripts in many languages for converting
> Roman Numerals into Numbers, but couldn't
> find one that I can use in a Rev Stack.

Here is the version I wrote some time ago:

function romanToDecimal pRoman
   -- I had this table stored in a CP, but I put it here to give a
self-contained function
   put "I,1" & cr into tCode
   put "V,5" & cr after tCode
   put "X,10" & cr after tCode
   put "L,50" & cr after tCode
   put "C,100" & cr after tCode
   put "D,500" & cr after tCode
   put "M,1000" & cr after tCode
   split tCode using cr and comma

   put 0 into tDecimal

   repeat with x = 1 to the number of chars in pRoman
      put char x of pRoman into tFirst
      put char x + 1 of pRoman into tSecond

      put tCode[tFirst] into tFirst
      put tCode[tSecond] into tSecond

      if tFirst >= tSecond then
         add tFirst to tDecimal
      else
         add tSecond - tFirst to tDecimal
         add 1 to x
      end if
   end repeat

   return tDecimal
end romanToDecimal


But I was never able to satisfactorily do the reverse. I could do the
basics but doing multiple subtractions of the preceding smaller value
didn't work.
So I could convert 4 to IV, but 9 converted to VIV instead of IX.

Cheers,
Sarah



More information about the use-livecode mailing list