The script below (and attached) will allow you to convert from a Roman Numeral to an Arabic Number (including negative sign). I saw Ira's converse function, but I needed to go the other way, so I found a sample at Rosetta Code and modified it to suit my needs.
CAVEAT: The code does not check Roman Numeral syntax. It will simply calculate any old letters you feed it, happily ignoring the muted groanings of Cato, Circero, all the Caesars and other Romans of note (or otherwise). One interesting feature, this means that you can plug your name in and see what your roman numeral is! Sounds like a stupid feature on Facebook, eh? My Roman Numeral is 49.
Any comments or suggestions for improvement are appreciated.
Remember to use the attached file to import it as a function, or paste this into a function declaration--not as a script!
The Code:
CAVEAT: The code does not check Roman Numeral syntax. It will simply calculate any old letters you feed it, happily ignoring the muted groanings of Cato, Circero, all the Caesars and other Romans of note (or otherwise). One interesting feature, this means that you can plug your name in and see what your roman numeral is! Sounds like a stupid feature on Facebook, eh? My Roman Numeral is 49.
Any comments or suggestions for improvement are appreciated.
Remember to use the attached file to import it as a function, or paste this into a function declaration--not as a script!
The Code:
Code:
'Date Created: 21-Nov-2011 11:32:51 AM 'Last Updated: 21-Nov-2011 11:32:51 AM 'Created By : spilon 'Updated By : spilon FUNCTION RomantoArabic AS N (romanNum AS C ) dim i as n dim n as n dim romanDigit as c dim lastval as n = 0 dim arabic as n = 0 dim isNeg as l '************************************************************************** '* NOTE WELL: This script does NOT check for proper Roman Numeral syntax. * '* You have been warned! * '************************************************************************** 'check for a negative number isNeg = iif(left(romanNum,1)="-", .t., .f.) 'loop through the Roman Numeral string in reverse order, grabbing the value for each "Roman Digit" for i = len(romanNum) to 1 step -1 romanDigit = ut(mid(romanNum, i, 1)) select case romanDigit = "M" n = 1000 case romanDigit = "D" n = 500 case romanDigit = "C" n = 100 case romanDigit = "L" n = 50 case romanDigit = "X" n = 10 case romanDigit = "V" n = 5 case romanDigit = "I" n = 1 case else n = 0 end select 'if the current "Roman Digit" is less than the previous one, subtract it, otherwise add it '(accounts for reverse-order processing of numbers like IX = 9) if n < lastval then arabic = arabic - n else arabic = arabic + n end if 'keep track of last "Roman Digit" value read lastval = n next i 'once loop is complete, output number to user (while accounting for negative numbers) RomantoArabic = iif(isNeg, 0 - arabic, arabic) END FUNCTION