Create Roman Numerals

I would like to share this for Xojo community, because I was not able to find it anywhere…

[code]Public Function Roman (N As Integer) As String

If N<=0 Then Return “”

Dim A() As Integer
Dim R() As String
Dim j As Integer

A = Array(1000, 500, 100, 50, 10, 5, 1)
R = Array(“M”, “D”, “C”, “L”, “X”, “V”, “I”)

For i As Integer = 0 To 6
If N = A(i) Then
Return R(i)
End If

``````For j = 6 To 2 step -2

If N + A(j) = A(i) Then
Return R(j) + R(i)
End If
Next
``````

Next

For i As Integer = 0 To 6
If N > A(i) Then
Return R(i) + Roman(N-A(i))
End If

``````j = 6
While (a(j) < N)
If N + A(j) > A(i) Then
Return R(j) + R(i) + Roman(N + A(j) - A(i))
End If
j = j - 2
Wend
``````

Next

End Function[/code]

If anyone by any chance discovers an error, please notify me. Thanks all.

Looks pretty good, thanks for sharing. One thing I noticed is it fails on edge cases of 49, 99, 490-499, 990-999, etc. Instead of outputting XLIX, XCIX, CDXCIX, etc is outputs IL, IC, XD-ID, XM-IM.

There are several ways to write these numbers (or at lease I think so). For example 999 can be both as IM, (one before thousand) or CMCXIX.

I believe the rule is that you use the smallest number of characters possible. Remember, they were literally chiseling these values in stone.

So It is quite right to write IL (49) instead XLIX… Spent less time with the chisel and a hammer…

Seems I’m remembering wrong…
Ones, tens, hundreds and thousands must always be separate, so XLIX (50-10) + (10 - 1) = 49

Superb - Can you imagine if we started submitting apps to the App Store in Roman numerals? I think Apple would have a fit!

Except the romans didn’t write 4 as IV 9 as IX etc - at least this subtractive form was not consistently used
4 was more often IIII
8 was more often IIII?III

The abbreviations we use now were not common in the Romans times

And then do you write MIM for 1999 ? If so why not MXM for 1990 ?

Keyser, Paul (1988). “The Origin of the Latin Numerals 1 to 1000”. American Journal of Archaeology 92: 529546.

I’ll dig up my History of Computation book as well as it has a segment on this

As Italian who studied latin, I can say that 4 is IV and 8 is VIII

This is code for VBA,

[code]Option Explicit

Function dec_to_roman(n As Integer) As String
Dim numero As Long, v As Variant, s As String, x As Variant

``````If n < 1 Or n > 3999 Then
dec_to_roman = "#VALORE!"
Exit Function
End If

v = Split(split_number(CStr(n)), ";")

For Each x In v
s = s & analizza(CInt(x))
Next

dec_to_roman = s
``````

End Function

Private Function split_number(n As String) As String
Dim s As String, i As Integer

``````For i = 1 To Len(n)
s = s & Mid(n, i, 1) * 10 ^ (Len(n) - i) & ";"
Next
split_number = Left(s, Len(s) - 1)
``````

End Function

Private Function analizza(n As Integer) As String
Dim s As String, i As Integer

``````Select Case n
Case Is > 900
i = n  1000
s = String(i, "M")

Case 100 To 900
i = n  100
Select Case i
Case 1 To 3
s = String(i, "C")

Case 4
s = "CD"

Case 5
s = "D"

Case 6 To 8
s = "D" & String(i - 5, "C")

Case 9
s = "CM"
End Select

Case 10 To 90
i = n  10
Select Case i
Case 1 To 3
s = String(i, "X")

Case 4
s = "XL"

Case 5
s = "L"

Case 6 To 8
s = "L" & String(i - 5, "X")

Case 9
s = "XC"
End Select

Case Else
i = n
Select Case i
Case 1 To 3
s = String(i, "I")

Case 4
s = "IV"

Case 5
s = "V"

Case 6 To 8
s = "V" & String(i - 5, "I")

Case 9
s = "IX"
End Select

End Select

analizza = s
``````

End Function[/code]

Proper latin, no doubt. But it would make sense that plebeians, especially in the provinces distant from Rome, used simplified forms just the same as today, in English or in French, spelling is steadily presented in slanguish spelling. Da computa is workin’

Wrong forms, even if widely (ab)used from plebeians, remains wrong.

Thx 4 postin but wots up wiv dat. lol

Wrong for purists, denatured Latin eventually became French or Spanish which are full fledged languages today. I would not dare speaking about other languages I do not know but the fact is “pure” Latin probably existed only in the capital. My point was not to say the folklorik local variations were right, but just that they probably existed.

Just like today Belgian and Swiss have different ways of saying “70” or “80” in French than the natives of France.

“Septente” in Belgium and Switzerland, for 70 “soixante dix” in France.
“Octante” in Switzerland, for 80 “quatre vingt” in France.

[quote=153047:@Michel Bujardet]
“Octante” in Switzerland, for 80 “quatre vingt” in France.[/quote]

Yeah thats because french used to count with fingers and they have only twenty.
Maybe the same reason for writing IIII instead if IV.

[quote=153060:@Massimo Valle]Yeah thats because french used to count with fingers and they have only twenty.
Maybe the same reason for writing IIII instead if IV.
:P[/quote]

The history of writing systems is full of artifacts and strange things along history. That is what makes it interesting.

I agree, and btw, I was kidding. I love France, Im in part french and I go often there.

It’s alright

Languages evolve differently in different countries because of local contexts, origins, and even political decisions. For example, French as it is known in France today had to be forced on the regions. Between 1880 and 1940, something like 40 laws were adopted in France to impose the “standard French” and effectively kill all regional dialects. French as it is spoken in Qubec today is an evolution of French as it existed in the northwest regions of France in the 15th, 16th and 17th centuries. The fact that Quebec was completely cut off from France during the British rule period that followed Montcalm’s defeat in Qubec City contributed largely to the isolated evolution of the spoken language in Qubec for many decades. Here is an interesting reference to the evolution of French in France.

Back to topic, I also learned the purist form of roman numbers in latin class, way too long ago.

can’t edit… I meant to add that it makes sense that differnet variations of roman numerals evolved in different regions over the centuries they were current, while a “central” version remained the reference, in the same way that languages evolve. My whole argument on French was an analogy of such evolution.