Anyone aware of a function or formula to convert Roman Numerals to a number?
There are plenty of calculators out there like this one
Roman Numeral Converter (calculatorsoup.com)
but I know of no way in Notes to convert Roman Numerals over 3999 because of the line that goes over the entire numeral that signifies that it needs to be multiplied by 1000. Otherwise, if you only need number less than 4000, it should be pretty straight forward to write.
How to Build a Roman Numeral Converter and an Interactive Roman Numerals Chart (freecodecamp.org)
From ChatGPT:
Function RomanToDecimal(roman As String) As Integer
Dim romanValues List As Integer
romanValues("I") = 1
romanValues("V") = 5
romanValues("X") = 10
romanValues("L") = 50
romanValues("C") = 100
romanValues("D") = 500
romanValues("M") = 1000
Dim decimalValue As Integer
Dim i As Integer
For i = 1 To Len(roman)
Dim currentSymbol As String
Dim nextSymbol As String
currentSymbol = Mid(roman, i, 1)
nextSymbol = ""
If i < Len(roman) Then
nextSymbol = Mid(roman, i + 1, 1)
End If
If romanValues(currentSymbol) < romanValues(nextSymbol) Then
decimalValue = decimalValue - romanValues(currentSymbol)
Else
decimalValue = decimalValue + romanValues(currentSymbol)
End If
Next
RomanToDecimal = decimalValue
End Function
Nice solution... unfortunately it will not work (like very much of the LotusScript code generated by ChatGPT) ... It will throw an error on the last character as then nextSymbol = "" and romanValues("") is not defined...
There has to be one more if around the calculation:
If nextSymbol <> "" Then
If romanValues(currentSymbol) < romanValues(nextSymbol) Then
decimalValue = decimalValue - romanValues(currentSymbol)
Else
decimalValue = decimalValue + romanValues(currentSymbol)
End If
Else
decimalValue = decimalValue + romanValues(currentSymbol)
End If
And of course one would need to "filter out" characters that are not roman by surrounding all of this with another if:
If IsElement( romanValues(currentSymbol) ) Then
If nextSymbol <> "" and IsElement( romanValues(currentSymbol) ) Then
If romanValues(currentSymbol) < romanValues(nextSymbol) Then
decimalValue = decimalValue - romanValues(currentSymbol)
Else
decimalValue = decimalValue + romanValues(currentSymbol)
End If
Else
decimalValue = decimalValue + romanValues(currentSymbol)
End If
End If
And this still fails if you have a string like IWX as it would result in 11 instead of 9.
So there is some more room for improvement....
This worked along with the changes @Torsten Link mentioned.
Hi Michael,
Is it like that your requirement is -->
1. To pass let's say some roman numerals and then Notes should fine its corresponding Integer on its own? 2. You do not wish to make Notes understand what does roman numerals really corresponds to which number by manual inputs in code etc. You want Notes to interpret it.
If yes, then there is no inbuilt API (Formula Language/LotusScript) method for same.
Regards,
Amit
Made a couple of adjustments to the code.
I set it up to skip the Next Roman Numeral if it is greater than the Current.
Example:
LXXXIX - the 4th X is skipped when processing the I
Dim bSkipCurrentLenCount As Boolean Dim iListRomanValues List As Integer Dim iLenCount As Integer, iLenCountRomNum As Integer, iDecimalValue As Integer Dim sRomanNumeral As String, sSymbolCurrent As String, sSymbolNext As String
On Error GoTo ErrorHandler
iListRomanValues(“I”) = 1
iListRomanValues(“V”) = 5
iListRomanValues(“X”) = 10
iListRomanValues(“L”) = 50
iListRomanValues(“C”) = 100
iListRomanValues(“D”) = 500
iListRomanValues(“M”) = 1000bSkipCurrentLenCount = False
iDecimalValue = 0
sRomanNumeral = “LXXXIX”
iLenCountRomNum = Len(sRomanNumeral)For iLenCount = 1 To iLenCountRomNum
If bSkipCurrentLenCount = True Then
bSkipCurrentLenCount = FalseGoTo NextLenCount End If sSymbolCurrent = Mid(sRomanNumeral, iLenCount, 1) sSymbolNext = Mid(sRomanNumeral, iLenCount + 1, 1) If sSymbolNext = "" Then iDecimalValue = iDecimalValue + iListRomanValues(sSymbolCurrent) Else If iListRomanValues(sSymbolCurrent) < iListRomanValues(sSymbolNext) Then iDecimalValue = iDecimalValue + (iListRomanValues(sSymbolNext) - iListRomanValues(sSymbolCurrent)) bSkipCurrentLenCount = True Else iDecimalValue = iDecimalValue + iListRomanValues(sSymbolCurrent) End If End If
NextLenCount:
NextIf iDecimalValue < 0 Then
iDecimalValue = 0
End IfGoTo ExitSub
ErrorHandler:
iDecimalValue = 0Resume ExitSub
ExitSub:
'Exit Sub