Task

Create a function taking a positive integer as its parameter and returning a string containing the Roman numeral representation of that integer. Modern Roman numerals are written by expressing each digit separately, starting with the left most digit and skipping any digit with a value of zero.



In Roman numerals:

 1990 is rendered: 1000=M, 900=CM, 90=XC; resulting in MCMXC
 2008 is written as 2000=MM, 8=VIII; or MMVIII
 1666 uses each Roman symbol in descending order: MDCLXVI


// Main program for testing the function
//
do {
    #1 = Get_Num("Number to convert: ", STATLINE)
    Call("NUM_TO_ROMAN")
    Num_Type(#1, NOCR) Message(" = ") Reg_Type(1) Type_Newline
} while (Reg_Size(1))
Return
 
// Convert numeric value into Roman number
//  #1 = number to convert; on return: T-reg(1) = Roman number
//
:NUM_TO_ROMAN:
    Reg_Empty(1)                        // @1 = Results (Roman number)
    if (#1 < 1) { Return }              // non-positive numbers return empty string
 
    Buf_Switch(Buf_Free)
    Ins_Text("M1000,CM900,D500,CD400,C100,XC90,L50,XL40,X10,IX9,V5,IV4,I1")
 
    BOF
    #2 = #1
    Repeat(ALL) {
        Search("|A|[|A]", ADVANCE+ERRBREAK)         // get next item from conversion list
        Reg_Copy_Block(20, CP-Chars_Matched, CP)    // @20 = Letter(s) to be inserted
        #11 = Num_Eval()                            // #11 = magnitude (1000...1)
        while (#2 >= #11) {
            Reg_Set(1, @20, APPEND)
            #2 -= #11
        }
    }
    Buf_Quit(OK)
Return