Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

# Convert Decimal to Roman Numerals

12 replies to this topic
• Members
• 475 posts
• Last active: Oct 21 2014 11:08 AM
• Joined: 31 Jul 2008
I couldn't find anything on the forums, so I came up with this:

```Alpha = M,CM,D,CD,C,XC,L,XL,X,IX,V,IV,I
Numeric = 1000,900,500,400,100,90,50,40,10,9,5,4,1
StringSplit, Alpha, Alpha, `,

Num = 3999

RomanString =
Loop, Parse, Numeric, `,
{
Count := A_Index
TempVar := Num // A_LoopField
Num -= TempVar * A_LoopField
Loop, %TempVar%
RomanString .= Alpha%Count%
}

MsgBox, %RomanString%```

It is in reality only accurate to 3999, because after that, the 'M' should have a bar over it, instead of 'MMMM'. I hope someone finds it useful.

• Moderators
• 4323 posts
• Last active: Jan 28 2016 01:38 AM
• Joined: 08 Apr 2009
* EDIT *
See this post: <!-- l --><a class="postlink-local" href="http://www.autohotke...p354957</a><!--l -->

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Antonio França -- git.io -- github.com -- ahk4.net

Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.

• Moderators
• 3165 posts
• Last active: Apr 01 2014 01:46 AM
• Joined: 07 Oct 2006
Somebody put one on RosettaCode: http://rosettacode.o...rals#AutoHotkey.

• Moderators
• 4713 posts
• Last active: Mar 31 2012 03:17 AM
• Joined: 14 Feb 2005
...which should have used Static variables, instead of assignments to locals:
```Roman(y) { ; write 0 < y < 4000 in Roman

Static romans = "M.CM.D.CD.C.XC.L.XL.X.IX.V.IV.I"

,M=1000,CM=900,D=500,CD=400,C=100,XC=90,L=50,XL=40,X=10,IX=9,V=5,IV=4,I=1

Loop Parse, romans, .

While y >= %A_LoopField%

r .= A_LoopField, y -= %A_LoopField%

Return r

}```

• Moderators
• 4323 posts
• Last active: Jan 28 2016 01:38 AM
• Joined: 08 Apr 2009
* EDIT *
See this post: <!-- l --><a class="postlink-local" href="http://www.autohotke...p354957</a><!--l -->

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Antonio França -- git.io -- github.com -- ahk4.net

Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.

• Members
• 475 posts
• Last active: Oct 21 2014 11:08 AM
• Joined: 31 Jul 2008

...which should have used Static variables, instead of assignments to locals:

```Roman(y) { ; write 0 < y < 4000 in Roman
Static romans = "M.CM.D.CD.C.XC.L.XL.X.IX.V.IV.I"
,M=1000,CM=900,D=500,CD=400,C=100,XC=90,L=50,XL=40,X=10,IX=9,V=5,IV=4,I=1
Loop Parse, romans, .
While y >= %A_LoopField%
r .= A_LoopField, y -= %A_LoopField%
Return r
}```

Nice Lazlo!

• Moderators
• 4323 posts
• Last active: Jan 28 2016 01:38 AM
• Joined: 08 Apr 2009
####################################################################################
This old forum will be archived ( see https://autohotkey.c...s-decommission/ ).
For my up-to-date code and information, check the following links:
http://git.io/master | https://github.com/MasterFocus | http://masterfocus.ahk4.net/ | https://sites.google...masterfocusahk/
####################################################################################

Functions:
Dec2Roman()
Roman2Dec()

Description:
A pair of functions created to perform convertions between decimal and roman numbers.

• As of 05/june/2010, Wikipedia showed 3,888,000 as [M][M][M][D][c][c][c][L][X][X][X][V]
• The representation used for 1,000 within the functions is [ M ], not [ I ]

GitHub (see AutoHotkey - Functions - RomanNumbers)

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Antonio França -- git.io -- github.com -- ahk4.net

Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.

• Members
• 202 posts
• Last active: Apr 12 2019 01:08 AM
• Joined: 30 Dec 2009
yay!!! I just went onto the forums praying someone had written this code. here it is! not too long ago, either. hope you don't mind if I coopted it for a script of my own, <!-- m -->http://www.autohotke... ... 459#360459<!-- m -->

Find me on the new AutoHotkey forums and send me a message if you have a question about any of the scrips I've posted to this forum!

• Moderators
• 4323 posts
• Last active: Jan 28 2016 01:38 AM
• Joined: 08 Apr 2009
Thanks for posting in this topic because now I can see that this code is incorrect (and the code for Roman2Dec is probably too). A few things are missing when declaring st_Romans. I'll try to fix that tomorrow!

EDIT: I quickly fixed it.
I'll edit my previous post and organize everything (including Roman2Dec's code) tomorrow.

* EDIT *
See this post: <!-- l --><a class="postlink-local" href="http://www.autohotke...p354957</a><!--l -->

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Antonio França -- git.io -- github.com -- ahk4.net

Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.

• Members
• 475 posts
• Last active: Oct 21 2014 11:08 AM
• Joined: 31 Jul 2008
Nice. How about a Roman2Dec function?

• Moderators
• 4323 posts
• Last active: Jan 28 2016 01:38 AM
• Joined: 08 Apr 2009

I'll edit my previous post and organize everything (including Roman2Dec's code) tomorrow.
Have you read the whole topic? :shock: This function has already been posted.
-> <!-- l --><a class="postlink-local" href="http://www.autohotke...p354957</a><!--l -->

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Antonio França -- git.io -- github.com -- ahk4.net

Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.

• Members
• 14 posts
• Last active: Oct 30 2010 04:00 PM
• Joined: 30 Mar 2010
Thanks for the functions, MasterFocus. Unfortunately, I couldn't get Roman2Dec to work at first. It just kept returning 0.

I dug into it and had it search and replace all spaces in l_Needle with bars (|); that way they would work as alternatives in the regex. That made the function work properly...

... except that it worked only with all-caps Roman numerals. So I added "i)" to the beginning of the regex to make it case-insensitive.

Here is the complete code:
[[ code removed - see github.com/MasterFocus ]]

• Members
• 17 posts
• Last active: Jan 30 2017 11:38 PM
• Joined: 17 Jul 2008

I couldn't get Roman2Dec to work at first. It just kept returning 0.

Now it is working...
Thanks