dcsimg
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: VB3, VB4, VB5, VB6
Expertise: Beginner
Aug 20, 2003

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


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 = sRtn
End Function
Steven Digby
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date