Sunday, April 30, 2017

Decimal To Roman Numeral in Visual Basic NET

Here is a sample program to ask the user to give a number and then it will convert in Roman Numeral equivalent using Microsoft Visual Basic NET.  The code is very short and easy to understand. 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 ConvertRoman(ByVal n As Integer) As String
        If n = 0 Then ConvertRoman = "0" : Exit Function
        Const r = "IVXLCDM"
        Dim i As Integer = Math.Abs(n)
        Dim s As String = ""

        For p As Integer = 1 To 5 Step 2
            Dim d As Integer = i Mod 10
            i = i \ 10
            Select Case d '
                Case 0 To 3 : s = s.PadLeft(d + Len(s), Mid(r, p, 1))
                Case 4 : s = Mid(r, p, 2) & s
                Case 5 To 8 : s = Mid(r, p + 1, 1) & s.PadLeft(d - 5 + Len(s), Mid(r, p, 1))
                Case 9 : s = Mid(r, p, 1) & Mid(r, p + 2, 1) & s
            End Select
        Next

        s = s.PadLeft(i + Len(s), "M")
        If n < 0 Then s = "-" & s
        ConvertRoman = s
    End Function
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Label2.Text = " The equivalent value of " & TextBox1.Text & " is " & ConvertRoman(TextBox1.Text) & "."
    End Sub

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

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




No comments:

Post a Comment