Code archives/Miscellaneous/Roman Numerals

This code has been declared by its author to be Public Domain code.

Download source code

Roman Numerals by Booticus2005
The INT passed to the romannumeral() function has to be 1-3000. Thats about it! I needed this piece of code to name planets in starsystems. Sol I, Sol II, Sol III, etc.
'***********************************************************************
'
'		         * * * * README * * * *
'			 **********************
'   Thanks for use this program. The use and distribution of this source
'code is totally FREE.
'   
'Manuel F Martínez
'Universidad Galileo
'Ingeniería en Electrónica, Informática y
'Ciencias de la Computación
'Guatemala Abril del 2001
'
' BlitzMax conversion by Mario Roberti. Enjoy!
' 5/14/05

' Print a text roman numeral, 1972 for example
print romannumeral(1972) ' Insert your number from 1 to 3000 in there!

Function romannumeral:String(arabicalNumeral:Int)
	Local result:Int              ' "result" store the position value           */
	Local roman:String
	' Obtain the value of thousands */
	If ((arabicalNumeral <= 3000) And (arabicalNumeral >= 1000))
		result = arabicalNumeral / 1000
		roman=roman+ a2roman(result, "M", " ", " ")
		arabicalNumeral :- (result * 1000)
	EndIf
	' Obtain the value of hundreds */
	If ((arabicalNumeral < 1000) And (arabicalNumeral >= 100))
		result = arabicalNumeral / 100
		roman=roman+a2roman(result, "C", "D", "M")
		arabicalNumeral :- (result * 100)
	EndIf
	' Obtain the value of tens */
	If ((arabicalNumeral < 100) And (arabicalNumeral >= 10))
		result = arabicalNumeral / 10
		roman=roman+a2roman(result, "X", "L", "C")
		arabicalNumeral :- (result * 10)
	EndIf
	' Obtain the value of units */
	If ((arabicalNumeral < 10) And (arabicalNumeral >= 1))
		roman=roman+a2roman(arabicalNumeral, "I", "V", "X")
	EndIf
	Return roman
End Function

Function a2roman:String(value:Int,c1:String,c2:String,c3:String)
	Local i:Int 	' "i" is the index of the iteration */
	Local rroman:String=""
	' If value = 1, 2, 3 */
	If ((value >= 1) And (value <= 3))
		For i = 1 To value
			rroman=rroman+c1
		Next 
	EndIf
	' If value = 5, 6, 7, 8 */
	If ((value >= 5) And (value <= 8))
		rroman=rroman+c2
		For i = 1 To value-5
			rroman=rroman+c1
		Next
	EndIf
	' If value = 4 */
	If (value = 4)
		rroman=rroman+c1
		rroman=rroman+c2
	EndIf
	' If value = 9 */
	If (value = 9)
		rroman=rroman+ c1
		rroman=rroman+ c3
	EndIf
	Return (rRoman)
End Function

Comments

Booticus2005
Oops! Guess I didn't need the [/codebox] stuff!


BlitzSupport2005
You should be able to edit them out. If not, let me know and I'll do it!


Code Archives Forum