Numbers To Roman

This algorithm converts decimal numbers to roman numerals.



									Public Shared Function NumbersToRoman(num As Integer) As String
	Dim roman As New StringBuilder()

	Dim totalM As Integer = Int(num \ 1000)
	num = num Mod 1000
	Dim totalCM As Integer = Int(num \ 900)
	num = num Mod 900

	Dim totalD As Integer = Int(num \ 500)
	num = num Mod 500
	Dim totalCD As Integer = Int(num \ 400)
	num = num Mod 400

	Dim totalC As Integer = Int(num \ 100)
	num = num Mod 100
	Dim totalXC As Integer = Int(num \ 90)
	num = num Mod 90

	Dim totalL As Integer = Int(num \ 50)
	num = num Mod 50
	Dim totalXL As Integer = Int(num \ 40)
	num = num Mod 40

	Dim totalX As Integer = Int(num \ 10)
	num = num Mod 10
	Dim totalIX As Integer = Int(num \ 9)
	num = num Mod 9

	Dim totalV As Integer = Int(num \ 5)
	num = num Mod 5
	Dim totalIV As Integer = Int(num \ 4)
	num = num Mod 4

	roman.Append(RepeatString("M", totalM))
	roman.Append(RepeatString("CM", totalCM))
	roman.Append(RepeatString("D", totalD))
	roman.Append(RepeatString("CD", totalCD))
	roman.Append(RepeatString("C", totalC))
	roman.Append(RepeatString("XC", totalXC))
	roman.Append(RepeatString("L", totalL))
	roman.Append(RepeatString("XL", totalXL))
	roman.Append(RepeatString("X", totalX))
	roman.Append(RepeatString("IX", totalIX))
	roman.Append(RepeatString("V", totalV))
	roman.Append(RepeatString("IV", totalIV))
	roman.Append(RepeatString("I", num))

	Return roman.ToString()
End Function

Private Shared Function RepeatString(s As String, count As Integer) As String
	If count < 0 Then
		Return String.Empty
	End If

	Dim str As New StringBuilder(count)

	For i As Integer = 0 To count - 1
		str.Append(s)
	Next

	Return str.ToString()
End Function
								


Example

									Dim data = 419
Dim value = NumbersToRoman(data)
								


Output

									CDXIX