VB.NET || Roman Numeral Conversion – How To Convert Roman Numeral To Integer & Integer To Roman Numeral
The following is a program with functions which demonstrates how to convert roman numerals to integer, and integers to roman numerals.
The sample program implemented on this page was presented in a Data Structures course. This program was assigned in order to practice the use of the class data structure.
1. Roman Numeral Conversion
The example below demonstrates how to convert integers to roman numerals and roman numerals to integers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 2, 2020 ' Taken From: http://programmingnotes.org/ ' File: romanNumeralConversion.vb ' Description: The following demonstrates how to convert roman numerals. ' ============================================================================ Option Strict On Option Explicit On Imports System Module Program Sub Main(args As String()) Try Dim decimalNumber = 1987 DecimalTest(decimalNumber) Display("") Dim roman = "MCMXCI" RomanTest(roman) Catch ex As Exception Display(ex.ToString) Finally Console.ReadLine() End Try End Sub Public Sub RomanTest(roman As String) Dim decimalNumber = RomanNumeral.ConvertToDecimal(roman) Dim convertedBack = RomanNumeral.ConvertToRoman(decimalNumber) Display("======= Roman Test Start =======") Display($"Original = {roman} Converted = {decimalNumber} Converted Back To Original = {convertedBack}") Display("======= Roman Test End =======") End Sub Public Sub DecimalTest(decimalNumber As Decimal) Dim roman = RomanNumeral.ConvertToRoman(decimalNumber) Dim convertedBack = RomanNumeral.ConvertToDecimal(roman) Display("======= Decimal Test Start =======") Display($"Original = {decimalNumber} Converted = {roman} Converted Back To Original = {convertedBack}") Display("======= Decimal Test End =======") End Sub ''' <summary> ''' Class to hold Roman Numeral Conversion Values ''' </summary> Public Class RomanNumeral Public Property [decimal] As Decimal Public Property roman As String ''' <summary> ''' Converts a Decimal number to a Roman Numeral ''' </summary> ''' <param name="decimal">Decimal number to be converted</param> Public Shared Function ConvertToRoman(decimalNumber As Decimal) As String Dim stbRoman = New Text.StringBuilder If decimalNumber > 0 Then Dim conversionValues = GetValues() For Each conversionValue In conversionValues While decimalNumber > 0 _ AndAlso decimalNumber >= conversionValue.decimal stbRoman.Append(conversionValue.roman) decimalNumber -= conversionValue.decimal End While If decimalNumber <= 0 Then Exit For End If Next End If Dim roman = stbRoman.ToString Return roman End Function ''' <summary> ''' Converts a Roman Numeral to a Decimal value ''' </summary> ''' <param name="roman">Roman Numeral value to be converted</param> Public Shared Function ConvertToDecimal(roman As String) As Decimal Dim decimalNumber As Decimal = 0 If Not String.IsNullOrWhiteSpace(roman) Then Dim previousNumber As Decimal = 0 Dim conversionValues = GetValues() ' Iterate the string starting from the end For index = roman.Length - 1 To 0 Step -1 Dim currentLetter = roman(index).ToString ' Skip whitepsace characters If String.IsNullOrWhiteSpace(currentLetter) Then Continue For End If ' Find a conversion value that matches the current letter Dim conversionValue = (From x In conversionValues Where x.roman.ToLower = currentLetter.ToLower Select x Take 1).FirstOrDefault ' If a valid conversion was found, get the value If conversionValue Is Nothing Then Continue For End If Dim currentNumber = conversionValue.decimal ' Calculate the result If previousNumber > currentNumber Then decimalNumber -= currentNumber Else decimalNumber += currentNumber End If ' Save the current number in order to process the next letter previousNumber = currentNumber Next End If Return decimalNumber End Function ''' <summary> ''' Returns Roman Numeral Conversion Values ''' </summary> Public Shared Function GetValues() As List(Of RomanNumeral) Dim conversionValues = New List(Of RomanNumeral) From { New RomanNumeral With {.decimal = 1000, .roman = "M"}, New RomanNumeral With {.decimal = 900, .roman = "CM"}, New RomanNumeral With {.decimal = 500, .roman = "D"}, New RomanNumeral With {.decimal = 400, .roman = "CD"}, New RomanNumeral With {.decimal = 100, .roman = "C"}, New RomanNumeral With {.decimal = 90, .roman = "XC"}, New RomanNumeral With {.decimal = 50, .roman = "L"}, New RomanNumeral With {.decimal = 40, .roman = "XL"}, New RomanNumeral With {.decimal = 10, .roman = "X"}, New RomanNumeral With {.decimal = 9, .roman = "IX"}, New RomanNumeral With {.decimal = 5, .roman = "V"}, New RomanNumeral With {.decimal = 4, .roman = "IV"}, New RomanNumeral With {.decimal = 1, .roman = "I"} } Return conversionValues.OrderByDescending(Function(x) x.decimal).ToList() End Function End Class Public Sub Display(message As String) Console.WriteLine(message) Debug.Print(message) End Sub End Module ' http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Once compiled, you should get this as your output
======= Decimal Test Start =======
Original = 1987
Converted = MCMLXXXVII
Converted Back To Original = 1987
======= Decimal Test End ============== Roman Test Start =======
Original = MCMXCI
Converted = 1991
Converted Back To Original = MCMXCI
======= Roman Test End =======
Leave a Reply