Important alert: (current site time 8/28/2014 12:46:48 AM EDT)
 

article

Roman Numeral Converting Formatting Function

Email
Submitted on: 7/2/2007 2:04:59 PM
By: Soren Winslow  
Level: Beginner
User Rating: By 1 Users
Compatibility: ASP (Active Server Pages), HTML, VbScript (browser/client side)
Views: 9342
author picture
(About the author)
 
     This is a function that will convert a number into Roman numerals. It takes a look at each place value then converts it into the appropriate roman mumeral value. This function also includes proper formatting of values of 4,999 up to 3,999,999,999.


 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				
Roman Numeral Converting Formatting Function
This is a function that will convert a number into Roman numerals. It takes a look at each place value then converts it into the appropriate roman mumeral value. This function also includes proper formatting of values of 4,999 up to 3,999,999,999.
Roman numerals only consist of I, V, X, L, C, D and M. A line over a letter value means that base value times 1000. A double line over a letter value means that base value times 100,000. Here are the place values of roman numerals.
I = 1
V = 5
X = 10
L = 50
C = 100
D = 500
M = 1,000
V = 5,000
X = 10,000
L = 50,000
C = 100,000
D = 500,000
M = 1,000,000
V = 5,000,000
X = 10,000,000
L = 50,000,000
C = 100,000,000
D = 500,000,000
M = 1,000,000,000

Here is the ASP code for this script:
<div style="text-align:left;padding-left:20px;">
<form name="GenRomNum" method="post" action="<%=Request.ServerVariables("URL")%>">
Input a number: <input type="text" name="TheNum" size="6">
<input type="submit" value="Convert">
</form>
</div>
<div class="breg" style="text-align:left;padding-left:20px;">
<%
 TheNum = Request("TheNum")
 RomNum = ""
 Function RomanNumeral(TheNum)
TheNum = CDbl(TheNum)
 SLine = "<span style=" & chr(34) & "text-decoration:overline;" & chr(34) & ">"
 DLine = "<span style=" & chr(34) & "border-top-style:double;" & chr(34) & ">"
R1 = "|I|II|III|IV|V|VI|VII|VIII|IX"
R10 = "|X|XX|XXX|XL|L|LX|LXX|LXXX|XC"
R100 = "|C|CC|CCC|CD|D|DC|DCC|DCCC|CM"
R1K = "|" & _
 "M|" & _
 "MM|" & _
 "MMM|" & _
 "M" & SLine & "V</span>|" & _
 SLine & "V</span>|" & _
 SLine & "V</span>M|" & _
 SLine & "V</span>MM|" & _
 SLine & "V</span>MMM|" & _
 "M" & SLine & "X</span>"
R10K = "|" & _
SLine & "X</span>|" & _
SLine & "XX</span>|" & _
SLine & "XXX</span>|" & _
SLine & "XL</span>|" & _
SLine & "L</span>|" & _
SLine & "LX</span>|" & _
SLine & "LXX</span>|" & _
SLine & "LXXX</span>|" & _
SLine & "XC</span>"
R100K = "|" & _
SLine & "C</span>|" & _
SLine & "CC</span>|" & _
SLine & "CCC</span>|" & _
SLine & "CD</span>|" & _
SLine & "D</span>|" & _
SLine & "DC</span>|" & _
SLine & "DCC</span>|" & _
SLine & "DCCC</span>|" & _
SLine & "CM</span>"
R1M = "|" & _
 SLine & "M</span>|" & _
 SLine & "MM</span>|" & _
 SLine & "MMM</span>|" & _
 SLine & "M</span>" & DLine & "V</span>|" & _
 DLine & "V</span>|" & _
 DLine & "V</span>" & SLine & "M</span>|" & _
 DLine & "V</span>" & SLine & "MM</span>|" & _
 DLine & "V</span>" & SLine & "MMM</span>|" & _
 DLine & "X</span>" & SLine & "M</span>"
R10M = "|" & _
DLine & "X</span>|" & _
DLine & "XX</span>|" & _
DLine & "XXX</span>|" & _
DLine & "XL</span>|" & _
DLine & "L</span>|" & _
DLine & "LX</span>|" & _
DLine & "LXX</span>|" & _
DLine & "LXXX</span>|" & _
DLine & "XC</span>"
R100M = "|" & _
DLine & "C</span>|" & _
DLine & "CC</span>|" & _
DLine & "CCC</span>|" & _
DLine & "CD</span>|" & _
DLine & "D</span>|" & _
DLine & "DC</span>|" & _
DLine & "DCC</span>|" & _
DLine & "DCCC</span>|" & _
DLine & "CM</span>"
R1B = DLine & "M</span>"
 PlaceVals = Array(R1,R10,R100,R1K,R10K,R100K,R1M,R10M,R100M)
 For x = 1 To Len(TheNum)
IntNum = Cint(Mid(TheNum,Len(TheNum)-(x-1),1))
If x < 10 Then
RNumArr = Split(PlaceVals(x-1),"|")
RomNum = RNumArr(IntNum) & RomNum
Else
For y = 1 to IntNum
RomNum = R1B & RomNum
Next
End If
 Next
 RomanNumeral = RomNum
 End Function
 If Len(Trim(TheNum)) > 0 Then
If IsNumeric(TheNum) = False Then
Response.Write "<b>Please enter a number</b>"
Else
Response.Write FormatNumber(TheNum,0) & " = " & RomanNumeral(TheNum)
End If
 End If
 
%>
</div>


Other 4 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.