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
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS