devxlogo

Return Roman Numerals

Return Roman Numerals

This VB procedure returns decimal numbers (integers) as Roman numerals (a string), ranging from 1 to 4999. Numbers outside this range return the same number as a string. The optional parameter iStyle allows two different numerical styles: standard (4 = iv, 9 = ix, and so on) when iStyle = -1, or classical (4 = iiii, 9 = viiii, and so on) when iStyle = -2.

The variable x should make the function more efficient, although you might not notice the time saved on a fast machine:

Public Function Roman(ByVal n As Integer, _	Optional iStyle As Integer = -1) As String	If n < 1 Or n >= 5000 Then 		Roman = CStr(n)		Exit Function	End If	If iStyle <> -2 Then iStyle = -1	Dim sRtn As String, i As Integer, x As Integer	Dim r(1 To 13) As String, v(1 To 13) As Integer	r(1) = "i": v(1) = 1	r(2) = "iv": v(2) = 4	r(3) = "v": v(3) = 5	r(4) = "ix": v(4) = 9	r(5) = "x": v(5) = 10	r(6) = "xl": v(6) = 40	r(7) = "l": v(7) = 50	r(8) = "xc": v(8) = 90	r(9) = "c": v(9) = 100	r(10) = "cd": v(10) = 400	r(11) = "d": v(11) = 500	r(12) = "cm": v(12) = 900	r(13) = "m": v(13) = 1000	x = UBound(v)	sRtn = ""	Do		For i = x To LBound(v) Step iStyle			If v(i) 7lt;= n Then				sRtn = sRtn & r(i)				n = n - v(i)				x = i				Exit For			End If		Next i	Loop Until n = 0	Roman = sRtnEnd Function
See also  5 Strategies for Supercharging Your CRM
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist