Last active
September 26, 2021 00:02
-
-
Save jpcranford/35098d7d201c33a673cec11bb46efbe3 to your computer and use it in GitHub Desktop.
Some quick AppleScript handlers to convert between roman numerals and integers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
romanToInt("mcdxcii") | |
--> 1492 | |
intToRoman(1492) | |
--> "MCDXCII" | |
on romanToInt(numeral) | |
set numeral to my TextHandlers's changeCaseOfText(numeral, "uppercase") | |
set n to 0 | |
try | |
repeat while numeral is not "" | |
if numeral starts with "M" then | |
set n to n + 1000 | |
set numeral to characters 2 thru (length of numeral) of numeral as string | |
else if numeral starts with "CM" then | |
set n to n + 900 | |
set numeral to characters 3 thru (length of numeral) of numeral as string | |
else if numeral starts with "D" then | |
set n to n + 500 | |
set numeral to characters 2 thru (length of numeral) of numeral as string | |
else if numeral starts with "CD" then | |
set n to n + 400 | |
set numeral to characters 3 thru (length of numeral) of numeral as string | |
else if numeral starts with "C" then | |
set n to n + 100 | |
set numeral to characters 2 thru (length of numeral) of numeral as string | |
else if numeral starts with "XC" then | |
set n to n + 90 | |
set numeral to characters 3 thru (length of numeral) of numeral as string | |
else if numeral starts with "L" then | |
set n to n + 50 | |
set numeral to characters 2 thru (length of numeral) of numeral as string | |
else if numeral starts with "XL" then | |
set n to n + 40 | |
set numeral to characters 3 thru (length of numeral) of numeral as string | |
else if numeral starts with "X" then | |
set n to n + 10 | |
set numeral to characters 2 thru (length of numeral) of numeral as string | |
else if numeral starts with "IX" then | |
set n to n + 9 | |
set numeral to characters 3 thru (length of numeral) of numeral as string | |
else if numeral starts with "V" then | |
set n to n + 5 | |
set numeral to characters 2 thru (length of numeral) of numeral as string | |
else if numeral starts with "IV" then | |
set n to n + 4 | |
set numeral to characters 3 thru (length of numeral) of numeral as string | |
else if numeral starts with "I" then | |
set n to n + 1 | |
set numeral to characters 2 thru (length of numeral) of numeral as string | |
end if | |
end repeat | |
end try | |
return n | |
end romanToInt | |
on intToRoman(n) | |
set str to {} | |
repeat until n = 0 | |
if n ≥ 1000 then | |
set the end of str to "M" | |
set n to n - 1000 | |
else if n ≥ 900 then | |
set the end of str to "CM" | |
set n to n - 900 | |
else if n ≥ 500 then | |
set the end of str to "D" | |
set n to n - 500 | |
else if n ≥ 400 then | |
set the end of str to "CD" | |
set n to n - 400 | |
else if n ≥ 100 then | |
set the end of str to "C" | |
set n to n - 100 | |
else if n ≥ 90 then | |
set the end of str to "XC" | |
set n to n - 90 | |
else if n ≥ 50 then | |
set the end of str to "L" | |
set n to n - 50 | |
else if n ≥ 40 then | |
set the end of str to "XL" | |
set n to n - 40 | |
else if n ≥ 10 then | |
set the end of str to "X" | |
set n to n - 10 | |
else if n ≥ 9 then | |
set the end of str to "IX" | |
set n to n - 9 | |
else if n ≥ 5 then | |
set the end of str to "V" | |
set n to n - 5 | |
else if n ≥ 4 then | |
set the end of str to "IV" | |
set n to n - 4 | |
else if n ≥ 1 then | |
set the end of str to "I" | |
set n to n - 1 | |
end if | |
end repeat | |
set AppleScript's text item delimiters to "" | |
return (str as string) | |
end intToRoman |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment