hi. so i need help in understanding this program.
its a hindu arabic - roman numeral converter.
i got it from some site.
i cant seem to understand how the whole process goes.
Const sMatrix As String = "I~V~X~L~C~D~M"
Private Function toroman(ByVal sDecNum As String) As String
Text1.Text = sDecNum
If sDecNum <> "0" And sDecNum <> vbNullString Then
If Len(sDecNum) > 3 Then text2.Text = String(Mid(sDecNum, 1, Len(sDecNum) - 3), "M")
If Len(sDecNum) > 2 Then text2.Text = text2.Text & GiveLetters(Mid(sDecNum, Len(sDecNum) - 2, 1), 4)
If Len(sDecNum) > 1 Then text2.Text = text2.Text & GiveLetters(Mid(sDecNum, Len(sDecNum) - 1, 1), 2)
text2.Text = text2.Text & GiveLetters(Mid(sDecNum, Len(sDecNum), 1), 0)
Else: text2.Text = "No Roman value for 0"
End If
End Function
Private Function GiveLetters(ByVal sInput As String, ByVal iArrStart As Integer) As String
Dim sLetterArray() As String
sLetterArray() = Split(sMatrix, "~")
Select Case sInput
Case 4: GiveLetters = sLetterArray(iArrStart) & sLetterArray(iArrStart + 1)
Case 5: GiveLetters = sLetterArray(iArrStart + 1)
Case 9: GiveLetters = sLetterArray(iArrStart) & sLetterArray(iArrStart + 2)
Case 6 To 8: GiveLetters = sLetterArray(iArrStart + 1) & String(sInput - 5, sLetterArray(iArrStart))
Case Else: GiveLetters = GiveLetters + String(sInput, sLetterArray(iArrStart))
End Select
End Function
Private Sub Command1_Click()
Dim sRoman As String
If Val(Text1.Text) < 1 Or Val(Text1.Text) > 3999 Then
MsgBox "Invalid Input. Please type in an integer from 0 - 3999 only!", _
vbExclamation
Text1.SetFocus
Text1.Text = ""
Exit Sub
Else
sRoman = toroman(Text1.Text)
End If
End Sub
here are my few questions about the program.
1. for instance. my input is 2345 to be converted to roman numeral which should be. MMCCCXLV
as we go over to the to roman function on the if statement :
If Len(sDecNum) > 3 Then text2.Text = String(Mid(sDecNum, 1, Len(sDecNum) - 3), "M")
it falls true here. so im not exactly sure how this whole line of code works. all i know is that this line should be able to display MM on text2.text.
as with this line of code :
If Len(sDecNum) > 2 Then text2.Text = text2.Text & GiveLetters(Mid(sDecNum, Len(sDecNum) - 2, 1), 4)
my inputs falls true on this again. but i cant seem to understand this. dont know what values are returned
but what im was thinking was this : GiveLetters(Mid(sDecNum, 3, 1), 4.lastly, im dont know when will an sinput fall on a case on my giveletter function.
i cant seem to understand how this function works.
thaaank you so much for the help!