Page 1 of 1
Converting Decimal Numbers to Roman Numerals
Posted: Fri Apr 05, 2013 11:12 am
by unixkd
Hi all
I need a function to Convert Decimal Numbers to Roman Numerals.
Thanks.
Joe
Re: Converting Decimal Numbers to Roman Numerals
Posted: Fri Apr 05, 2013 6:46 pm
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