[Openroad-users] Convert numbers to Roman
Hervé de Gabai
hdgcontact-2007 at yahoo.co.uk
Mon Jul 21 22:36:00 EST 2008
Dear all,
I have finally written an OpenRoad Procedure to convert a Roman number to an Integer number.
I have tested it up to number 3999.
The procedure assumes that the Roman number is properly formatted.
procedure RomanToInteger_prc ( prm_Roman = varchar(20) not null ) =
/* Accepts a properly formatted Roman number */
/* Returns -1 in case an invalid character is found */
/* Returns the equivalent Integer number */
declare
#DEFINE $i 1
#DEFINE $v 5
#DEFINE $x 10
#DEFINE $l 50
#DEFINE $c 100
#DEFINE $d 500
#DEFINE $m 1000
ar_Int = Array of IntegerObject;
lcl_i = integer not null;
lcl_RomanChar = char(1) not null;
lcl_Result = integer not null;
enddeclare
begin
prm_Roman = lowercase(squeeze(prm_Roman));
for lcl_i = 1 to length(prm_roman) do
lcl_RomanChar = charextract (prm_roman, lcl_i);
if lcl_RomanChar = 'i' then
ar_Int[lcl_i].value = $i;
elseif lcl_RomanChar = 'v' then
ar_Int[lcl_i].value = $v;
elseif lcl_RomanChar = 'x' then
ar_Int[lcl_i].value = $x;
elseif lcl_RomanChar = 'l' then
ar_Int[lcl_i].value = $l;
elseif lcl_RomanChar = 'c' then
ar_Int[lcl_i].value = $c;
elseif lcl_RomanChar = 'd' then
ar_Int[lcl_i].value = $d;
elseif lcl_RomanChar = 'm' then
ar_Int[lcl_i].value = $m;
else
return -1;
endif;
if lcl_i > 1 then
if ar_Int[lcl_i - 1].value < ar_Int[lcl_i].value then
ar_Int[lcl_i - 1].value = -1 * ar_Int[lcl_i - 1].value
endif;
endif;
endfor;
for lcl_i = 1 to ar_Int.LastRow do
lcl_Result = lcl_Result + ar_Int[lcl_i].value;
endfor;
return lcl_Result;
end;
----- Original Message ----
From: "gareth.2.edwards at bt.com" <gareth.2.edwards at bt.com>
To: openroad-users at peerlessit.com
Sent: Tuesday, 8 July, 2008 12:11:28 PM
Subject: Re: [Openroad-users] Convert numbers to Roman
Hi Herve,
Here's a procedure i just knocked up for you. You need to parse the RomanNumeral string prior to calling the procedure to check it comprises of correct characters.
PROCEDURE roman2int(RomanNumeral = VARCHAR(10) NOT NULL)=
DECLARE
Symbol = CHAR(1) NOT NULL;
Year = SMALLINT NOT NULL DEFAULT 0;
ENDDECLARE
{
WHILE RomanNumeral != '' DO
Symbol = LEFT(RomanNumeral,1);
IF Symbol = 'I' THEN
Year = Year + 1;
ELSEIF Symbol = 'V' THEN
Year = Year + 5;
ELSEIF Symbol = 'X' THEN
Year = Year + 10;
ELSEIF Symbol = 'L' THEN
Year = Year + 50;
ELSEIF Symbol = 'C' THEN
Year = Year + 100;
ELSEIF Symbol = 'D' THEN
Year = Year + 500;
ELSE // Character = 'M'
Year = Year + 1000;
ENDIF;
RomanNumeral = RIGHT(RomanNumeral,LENGTH(RomanNumeral) - 1);
ENDWHILE;
RETURN Year;
}
Cheers,
Gareth Edwards
BT Global Services
t: +44 (0)131 300 1907
e: Gareth.2.Edwards at BT.com
________________________________
From: openroad-users-bounces at peerlessit.com [mailto:openroad-users-bounces at peerlessit.com] On Behalf Of Hervé de Gabai
Sent: Tuesday, July 08, 2008 7:38 AM
To: International OpenROAD Users
Subject: Re: [Openroad-users] Convert numbers to Roman
Good Morning all,
Thank you Bodo for the int2roman procedure which might prove useful.
Do you also have an OpenRoad Procedure to convert roman to integer?
Regards
Hervé de Gabai
Applications Systems Division
International Telecommunications Union
----- Original Message ----
From: Bodo Bergmann <Bodo.Bergmann at ingres.com>
To: International OpenROAD Users <openroad-users at peerlessit.com>
Sent: Wednesday, 2 July, 2008 1:51:15 PM
Subject: Re: [Openroad-users] Convert numbers to Roman
Have you tried something like this (modiefied from some VB example I found - should work for numbers<4000):
PROCEDURE int2roman(iNumber=INTEGER NOT NULL)=
DECLARE
sNumeral=VARCHAR(4) NOT NULL;
ENDDECLARE
{
WHILE (iNumber > 0) DO
IF iNumber >= 1000 THEN
sNumeral = sNumeral + 'M';
iNumber = iNumber - 1000;
ELSEIF iNumber >= 900 THEN
sNumeral = sNumeral + 'CM';
iNumber = iNumber - 900;
ELSEIF iNumber >= 500 THEN
sNumeral = sNumeral + 'D';
iNumber = iNumber - 500;
ELSEIF iNumber >= 400 THEN
sNumeral = sNumeral + 'CD';
iNumber = iNumber - 400;
ELSEIF iNumber >= 100 THEN
sNumeral = sNumeral + 'C';
iNumber = iNumber - 100;
ELSEIF iNumber >= 90 THEN
sNumeral = sNumeral + 'XC';
iNumber = iNumber - 90;
ELSEIF iNumber >= 50 THEN
sNumeral = sNumeral + 'L';
iNumber = iNumber - 50;
ELSEIF iNumber >= 40 THEN
sNumeral = sNumeral + 'XL';
iNumber = iNumber - 40;
ELSEIF iNumber >= 10 THEN
sNumeral = sNumeral + 'X';
iNumber = iNumber - 10;
ELSEIF iNumber = 9 THEN
sNumeral = sNumeral + 'IX';
iNumber = 0;
ELSEIF iNumber >= 5 THEN
sNumeral = sNumeral + 'V';
iNumber = iNumber - 5;
ELSEIF iNumber = 4 THEN
sNumeral = sNumeral + 'IV';
iNumber = 0;
Else
sNumeral = sNumeral + 'I';
iNumber = iNumber - 1;
ENDIF;
ENDWHILE;
RETURN sNumeral;
}
You can of course apply lowercase() to the return value if required.
Bodo.
Bodo Bergmann| Sr. Software Engineer | Bodo.Bergmann at ingres.com| Ingres Germany GmbH | Ohmstr. 12 | 63225 Langen | GERMANY | +49 6103 9881 0
________________________________
Not happy with your email address?
Get the one you really want - millions of new email addresses available now at Yahoo!
__________________________________________________________
Not happy with your email address?.
Get the one you really want - millions of new email addresses available now at Yahoo! http://uk.docs.yahoo.com/ymail/new.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.peerlessit.com/pipermail/openroad-users/attachments/20080721/f7b6b6ce/attachment.html
More information about the Openroad-users
mailing list