miércoles, 25 de enero de 2012

Convertir números decimales a números romanos en vb.net



Public Class Frm05
    Dim NumRomano As String
    Private Sub Button1_Click(ByVal sender As System.ObjectByVal e AsSystem.EventArgsHandles Button1.Click
        If Not IsNumeric(TxtNumero.Text) Then
            MsgBox("Ingrese Numero")
            Exit Sub
        End If
        If Val(TxtNumero.Text) > 3000 Then
            MsgBox("Ingrese Numero menor a 3000")
            Exit Sub
        End If
        Try
            lblromano.Text = ""
            RomanNumber(Val(TxtNumero.Text))
            lblromano.Text = NumRomano
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Public Function RomanNumber(ByVal Numero As IntegerAs String
        NumRomano = ""
        Try
            Do While Numero > 0
                Select Case Numero
                    Case Is >= 1000
                        NumRomano &= "M"
                        Numero -= 1000
                    Case Is >= 900
                        NumRomano &= "CM"
                        Numero -= 900
                    Case Is >= 500
                        NumRomano &= "D"
                        Numero -= 500
                    Case Is >= 400
                        NumRomano &= "CD"
                        Numero -= 400
                    Case Is >= 100
                        NumRomano &= "C"
                        Numero -= 100
                    Case Is >= 90
                        NumRomano &= "XC"
                        Numero -= 90
                    Case Is >= 50
                        NumRomano &= "L"
                        Numero -= 50
                    Case Is >= 40
                        NumRomano &= "XL"
                        Numero -= 40
                    Case Is >= 10
                        NumRomano &= "X"
                        Numero -= 10
                    Case 9
                        NumRomano &= "IX"
                        Numero -= 9
                    Case 5 To 8
                        NumRomano &= "V"
                        Numero -= 5
                    Case 4
                        NumRomano &= "IV"
                        Numero -= 4
                    Case 1 To 3
                        NumRomano &= "I"
                        Numero -= 1
                End Select
            Loop
            Return NumRomano
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Emint msgbox...!")
        End Try
    End Function
End Class

1 comentario: