Conversion to roman numerals

Hi,

I need to create a script that converts a number to a roman numeral. I haven’t a clue as to how to even begin. Are there any snippets out there to start out with?

Thanks in advance

I’m sure there’s a better way to do this. This will only work up to a point but for numbers 1 through 4000 or so it should be OK:

set adRunListPath to (titleFolder & ":Display Ad:")
set adList to list folder adRunListPath
set dropDownList to every item of adList --whose file type is {"TEXT"}
choose from list dropDownList with prompt "Choose the Export for this section."
set chosenItem to result
if chosenItem is false then
	error number -128
else
	set filepath to (adRunListPath & chosenItem)
	set aList to read file filepath as text using delimiter return
end if

Jon

If there is a better way I would not know Jon…your script is nice and short.
I found a couple other examples on the net elsewhere but they were long in length and hard to understand.
I really appreciate your help.

Jon’s method knocks the socks off the beautiful algorithm I was going to offer. :frowning:

.which makes me uncharitable enough to point out that there’s no need to treat the input number textually, and thus no need for a ‘try’ block: :wink:

property Roman_hundreds : {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}
property Roman_tens : {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}
property Roman_ones : {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}

on convert_to_Roman(n)
	set the_one to n div 1 mod 10
	set the_ten to n div 10 mod 10
	set the_hundred to n div 100 mod 10
	set the_thousand to ""
	repeat (n div 1000) times
		set the_thousand to the_thousand & "M"
	end repeat
	return (the_thousand & (Roman_hundreds's item (the_hundred + 1)) & (Roman_tens's item (the_ten + 1)) & (Roman_ones's item (the_one + 1)))
end convert_to_Roman

Man you guys produce some tight code.
Put’s my meager stuff to shame.
Thanks again for the responses.

Well, if you’re going to do it that way, why even assign variables? Also, let’s speed it up by appending the thousands to a list instead of concatenating the string:

property Roman_hundreds : {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}
property Roman_tens : {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}
property Roman_ones : {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}

set the_year to my convert_to_Roman(2004)

on convert_to_Roman(n)
	set the_thousand to {}
	repeat (n div 1000) times
		set end of the_thousand to "M"
	end repeat
	return ("" & the_thousand & (Roman_hundreds's item ((n div 100 mod 10) + 1)) & (Roman_tens's item ((n div 10 mod 10) + 1)) & (Roman_ones's item ((n div 1 mod 10) + 1)))
end convert_to_Roman

Jon

If you want to do it without pre-calculating the Roman lists, try this:

set the_year to my convert_to_Roman(2004)
-->"MMIV"

on convert_to_Roman(n)
	set m to {}
	repeat n div 1000 times
		set end of m to "M"
	end repeat
	return "" & m & my digit_to_Roman(n mod 1000 div 100, "C", "D", "M") & my digit_to_Roman(n mod 100 div 10, "X", "L", "C") & my digit_to_Roman(n mod 10, "I", "V", "X")
end convert_to_Roman

on digit_to_Roman(n, r1, r5, r10)
	if n = 0 then return ""
	if n = 9 then return r1 & r10
	if n is greater than or equal to 5 then return r5 & digit_to_Roman(n - 5, r1, r5, r10)
	return item n of {r1, r1 & r1, r1 & r1 & r1, r1 & r5}
end digit_to_Roman

Jon

Beter and better. :slight_smile: Still brief and a good performer.

My own effort, if you’re interested, had its own strengths and weaknesses.

on RomanNum(n, upper) -- takes string or number; upper is 'true' for upper case, 'false' for lower case
	set n to n as number
	if n < 1 or n > 3999 or n is not n div 1 then
		error "Hunc numerum facere non possum!"
	end if
	set numerals to {"I", "V", "X", "L", "C", "D", "M", "i", "v", "x", "l", "c", "d", "m"}
	
	set Roman to {}
	set i to (count numerals) div 2 + 1
	if upper then set i to 1
	repeat until n = 0
		set dec to n mod 10
		set ixcm to item i of numerals
		repeat dec mod 5 mod 4 times
			set beginning of Roman to ixcm
		end repeat
		if dec > 3 then set beginning of Roman to item (i + (dec + 1) div 5) of numerals
		if dec mod 5 is 4 then set beginning of Roman to ixcm
		set i to i + 2
		set n to n div 10
	end repeat
	
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ""
	set Roman to Roman as string
	set AppleScript's text item delimiters to astid
	
	return Roman
end RomanNum

RomanNum(1999, true) -- 'true' for upper case
--> "MCMXCIX"

RomanNum(37, false) -- 'false' for lower case
--> "xxxvii"

Does anyone know if there are any Roman numerals beyond M?

thanks for the brain tickle
inspired i had a look at this

http://members.aol.com/panicYr00/MCMXCIX.html