PDA

View Full Version : Conversion to Roman Numerals

ABC123

csda1
06-30-2005, 09:11 AM
Attached and shown here is a file containing a function to convert to Roman Numerals

Regards,

Ira
=================================================================

'Date Created: 30-Jun-2005 11:40:20 AM
'Last Updated: 30-Jun-2005 01:05:12 PM
'Created By : Ira J. Perlow
'Updated By : Ira J. Perlow
FUNCTION RomanNumeral AS C (Numb as N)
'DESCRIPTION Convert a number to a roman numeral (Maximum is 3999)

' Created by Computer Systems Design &amp; Associates

' Purpose: This function converts a number to a roman numeral
' Input: Numb = Number between -3999 and 3999 to convert to Roman Numeral.
' Numbers outside this range are returned as null ""
' A zero value is returned as a single space " "
' Decimal values are ignored

' Output: RomanNumeral = Value as a character Roman Numeral, a single space, or a null

' Errors: If number is less than -3999 or greater than 3999

IF .not.(between(INT(numb),-3999,3999))
' Roman numerals not defined above and below these numbers, so return null
RomanNumeral=""
END
ELSE IF numb=0
' Return single space if 0
RomanNumeral=" "
END
END IF

' Roman numeral values are converted as follows
' "I"=1
' "V"=5
' "X"=10
' "L"=50
' "C"=100
' "D"=500
' "M"=1000
' Numbers larger than this, uses a bar above the character to indicate the value times 1000

' These are the translation strings for ones, tens, hundreds and thousands
' which are embedded in the expression for speed
' N1=",I,II,III,IV,V,VI,VII,VIII,IX"
' N10=",X,XX,XXX,XL,L,LX,LXX,LXXX,XC"
' N100=",C,CC,CCC,CD,D,DC,DCC,DCCC,CM"
' N1000=",M,MM,MMM"

' Get string equivalent of absolute value of number
snumb=STR(abs(numb),25,0)

' Get length of string
lnumb=len(snumb)

' Convert to Roman Numeral
RomanNumeral=IF(numb"0,"-","")+WORD(",M,MM,MMM",VAL(SUBSTR(snumb,lnumb-3,1))+1,",")\
+WORD(",C,CC,CCC,CD,D,DC,DCC,DCCC,CM",VAL(SUBSTR(snumb,lnumb-2,1))+1,",")\
+WORD(",X,XX,XXX,XL,L,LX,LXX,LXXX,XC",VAL(SUBSTR(snumb,lnumb-1,1))+1,",")\
+WORD(",I,II,III,IV,V,VI,VII,VIII,IX",VAL(SUBSTR(snumb,lnumb,1))+1,",")

END

END FUNCTION