Roman Numeral Conversion

OpenScript ››

PROBLEM

I'm looking for an OpenScript function which will convert a Number to a Roman Numeral, and also convert a Roman Numeral back into a Number.

NOTE

OpenScript is a programming language within ToolBook. It is more powerful than the Actions Editor programming language, however it only works within Native ToolBook. OpenScript does not function within a Web Published lesson.


ANSWER

ToolBook does not have built-in functions which will convert between Numbers and Roman Numerals.


This means you'll need to write your own functions to do the conversions. If you are not up to the challenge of writing those two functions, you can use the ones below, written by ToolBook Technical Support.

Converting from a Number to Roman Numeral


Use the following format to call the function:


x = ConvertToNumber("XIV")


Put this function in your script for the book:


to get ConvertToNumber Roman

  put "0" after Roman

  Rroman = "0,I,V,X,L,C,D,M"

  Rvalues = "0,1,5,10,50,100,500,1000"

  finished = 0

  step k from 1 to charCount(Roman)

    Ref2 = char k + 1 of Roman

    Ref3 = itemoffset(char k of Roman,Rroman)

    if Ref3 >= itemoffset(Ref2,Rroman)

      increment finished by item Ref3 of Rvalues

    else

      decrement finished by item Ref3 of Rvalues

    end

  end

  return finished

end

Converting from a Roman Numeral to a Number


Use the following format to call the function:


x = ConvertToRoman(14)


Put this function in your script for the book:


to get ConvertToRoman num

  if num = null

     return null

  end

  roman = "I,IV,V,IX,X,XL,IL,L,XC,IC,C,CD,SD,ID,D,CM,XM,IM,M"

  rValues = "1,4,5,9,10,40,49,50,90,99,100,400,490,499,500,900,990,999,1000"

  finished = ""

  step k from itemCount(rValues) to 1 by - 1

     curValue = item k of rValues

     while num >= curValue

        decrement num by curValue

        put item k of roman after finished

     end

  end

  return finished

end



Created with the Personal Edition of HelpNDoc: Full-featured Help generator