Converting Decimal Numbers to Roman Numerals

This forum is for eXpress++ general support.
Post Reply
Message
Author
User avatar
unixkd
Posts: 565
Joined: Thu Feb 11, 2010 1:39 pm

Converting Decimal Numbers to Roman Numerals

#1 Post by unixkd »

Hi all

I need a function to Convert Decimal Numbers to Roman Numerals.

Thanks.

Joe

User avatar
rdonnay
Site Admin
Posts: 4729
Joined: Wed Jan 27, 2010 6:58 pm
Location: Boise, Idaho USA
Contact:

Re: Converting Decimal Numbers to Roman Numerals

#2 Post by rdonnay »

Code: Select all

/*
I found the following algorithm and converted it to code:

For decimal number x:

    From the following table, find the highest decimal value v that is less than or equal to the decimal number x
    and its corresponding roman numeral n:

    1       I
    4       IV
    5       V
    9       IX
    10      X
    40      XL
    50      L
    90      XC
    100     C
    400     CD
    500     D
    900     CM
    1000    M

     
    Write the roman numeral n that you found and subtract its value v from x:

    x = x - v

    Repeat stages 1 and 2 until you get zero result of x.
*/

FUNCTION Dec2Roman( nDec )

LOCAL i, aRoman, cRoman := ''

aRoman := { ;
    {"M", 1000}, ;
    {"CM", 900}, ;
    {"D",  500}, ;
    {"CD", 400}, ;
    {"C",  100}, ;
    {"XC",  90}, ;
    {"L",   50}, ;
    {"XL",  40}, ;
    {"X",   10}, ;
    {"IX",   9}, ;
    {"V",    5}, ;
    {"IV",   4}, ;
    {"I",    1} }

DO WHILE nDec > 0
  FOR i := 1 TO 13
    IF aRoman[i,2] <= nDec
      cRoman += aRoman[i,1]
      nDec -= aRoman[i,2]
      EXIT
    ENDIF
  NEXT
ENDDO

RETURN cRoman
The eXpress train is coming - and it has more cars.

Post Reply