Wednesday, June 14, 2017

Decimal To Roman Numeral in VB.NET

Here a sample program that I wrote in VB.NET to accept a decimal input value and then it will convert it to roman numeral equivalent the code is very simple and easy to understand. I hope you will learn from it. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.





Sample Program Output


Program Listing

Public Class Form1

    Private Function ConvertToRoman(ByVal input As Integer) As String
        Dim numeral As String = String.Empty

        If input < 1 OrElse input > 4000 Then

            Throw New ArgumentOutOfRangeException("input", input.ToString, "Value must be greater than 0 and less than 4,001.")
        Else

            Dim numeralDic As Dictionary(Of Integer, String) = New Dictionary(Of Integer, String)
            With numeralDic
                .Add(1, "I")
                .Add(4, "IV")
                .Add(5, "V")
                .Add(9, "IX")
                .Add(10, "X")
                .Add(40, "XL")
                .Add(50, "L")
                .Add(90, "XC")
                .Add(100, "C")
                .Add(400, "CD")
                .Add(500, "D")
                .Add(900, "CM")
            End With


            For x As Integer = 0 To input.ToString.Length - 1
                Dim currentValue As Integer = CInt(input.ToString.Substring(x, 1))

                If x = input.ToString.Length - 1 Then

                    If numeralDic.ContainsKey(currentValue) Then

                        numeral &= numeralDic(currentValue)
                    ElseIf currentValue < 4 Then
                    
                        For y As Integer = 1 To currentValue
                            numeral &= "I"
                        Next
                    ElseIf currentValue > 5 Then

                        numeral &= "V"
                        For y As Integer = 6 To currentValue
                            numeral &= "I"
                        Next
                    End If
                ElseIf x = input.ToString.Length - 2 Then
                   
                    currentValue = currentValue * 10
                    If numeralDic.ContainsKey(currentValue) Then
                        numeral &= numeralDic(currentValue)
                    ElseIf currentValue < 4 Then
                        For y As Integer = 1 To currentValue
                            numeral &= "X"
                        Next
                    ElseIf currentValue > 5 Then
                        numeral &= "L"
                        For y As Integer = 6 To currentValue
                            numeral &= "X"
                        Next
                    End If
                ElseIf x = input.ToString.Length - 3 Then
                    currentValue = currentValue * 100
                    If numeralDic.ContainsKey(currentValue) Then
                        numeral &= numeralDic(currentValue)
                    ElseIf currentValue < 4 Then
                        For y As Integer = 1 To currentValue
                            numeral &= "C"
                        Next
                    ElseIf currentValue > 5 Then
                        numeral &= "D"
                        For y As Integer = 6 To currentValue
                            numeral &= "C"
                        Next
                    End If
                Else

                    For y As Integer = 1 To currentValue
                        numeral &= "M"
                    Next
                End If
            Next
        End If


        Return numeral
    End Function
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        End
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label3.Text = "The Roman Numeral Equivalent of " & Val(TextBox1.Text) & " is " & ConvertToRoman(Val(TextBox1.Text)) & "."


    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.Text = ""
        Label3.Text = ""
        TextBox1.Focus()

    End Sub
End Class


No comments:

Post a Comment