Roman No.

  • Thread starter Thread starter karim
  • Start date Start date
K

karim

Hello All,
I'm trying to have an app that gives you roman numbers when you
enter a normal #. any ideas? i can have an if statment that when you put the
#1 it gives you I, 2 will be = II...etc. but is there a way to make it
calculate? so that instead of having it saved in the form, it find out what
the roman # should be and displays it. like this: the #4=IV...instead of
saving that, the form should calculate 5-1 which is V-I which = IV....

I hope I explained it good. thanks for your help...
 
This article at CodeProject has a variety of number conversion methods in C#
(http://www.codeproject.com/KB/cs/numberconvert.aspx). Below is just the
Roman numerals one in VB.NET. Note that only positive values up to 3998 are
valid in this numeral system.

Public Function ToRomanNumeral(ByVal value As Short) As String
Const MAX As Short = 3998

If value > MAX Then
Throw New ArgumentOutOfRangeException("value")
End If

Dim romanDigits As String(,) = New String(,) { _
{"M", "C", "X", "I"}, _
{"MM", "CC", "XX", "II"}, _
{"MMM", "CCC", "XXX", "III"}, _
{Nothing, "CD", "XL", "IV"}, _
{Nothing, "D", "L", "V"}, _
{Nothing, "DC", "LX", "VI"}, _
{Nothing, "DCC", "LXX", "VII"}, _
{Nothing, "DCCC", "LXXX", "VIII"}, _
{Nothing, "CM", "XC", "IX"} _
}

Dim result As New StringBuilder(15)

For index As Integer = 0 To 3
Dim power As Integer = CInt(Math.Pow(10, 3 - index))
Dim digit As Integer = value / power

value -= digit * power

If digit > 0 Then
result.Append(romanDigits(digit - 1, index))
End If
Next

Return result.ToString()
End Function
 
karim said:
Hello All,
I'm trying to have an app that gives you roman numbers when you
enter a normal #. any ideas? i can have an if statment that when you put the
#1 it gives you I, 2 will be = II...etc. but is there a way to make it
calculate? so that instead of having it saved in the form, it find out what
the roman # should be and displays it. like this: the #4=IV...instead of
saving that, the form should calculate 5-1 which is V-I which = IV....

I hope I explained it good. thanks for your help...

Instead of trying to make someone else do your homework for you, I
advice you to try to solve it yourself. The whole purpose of the
homework is for you to learn how to solve problems yoruself. If you skip
this, you will only be further behind when you get the next assignment.

If you just make an effort to the solve the problem, you can ask about
your code in a newsgroup. If you clearly explain that it is a homework
assignment and show what you have done to try to solve it, you will find
that people are surprisingly helpful.
 
Back
Top