Roman Numeral Generator Part 2

Hi guys and welcome to part two of this mini tutorial series. In the last part we created a method which converted numbers from 1-10 into roman numerals. In this part we will extend it so we can incorporate higher numbers.

So what are the numerals for the higher numbers?

Number Numeral
11 XI
12 XII
14 XIV
15 XV
20 XX
30 XXX
40 XL
50 L
60 LX
70 LXX
90 XC
100 C
500 D
1000 M

Ok so let’s get stuck in. Open up the RomanNumeral script we created last time.

We are going to move what we did last time to its own method. Call this method NumeralsOneToTen and pass it an int. Make it public and static.

Setting Up NumeralsOneToTen

Now cut and paste all of RomanNumeralGenerator into the new method.

NumeralsOneToTen Complete

In the RomanNumeralGenerator method declare a string called roman again, set it equal to “” and then return it.

Declaring and Returning roman


We will take a slightly different approach with the higher numbers. Instead of using switch/case statements we will be using if/else statements. The reason for this is because switch/case the case has to be an absolute (i.e. we would have to write a case for every number up to infinity… which would take a very very long time :)) where as if/else uses conditions (like less than, greater than etc…). I have used the switch/case approach for 1 to 10 purely because I believe those would be the most common numbers passed to the generator. It also introduces anyone who is a beginner to switch/case.

Create a new if statement with the condition number is greater than or equal to 1000. In the if statement set roman equal to roman + “M”.

If Statement

Add in similar else if statements for the following numbers and their corresponding numerals:

900, 500, 400, 100, 90, 50 and 40

Else Ifs

Two more else if statements are required for this to work. Add another if statement for greater than 10. Have it add an X to roman. Then add one for less than or equal to 10. In this one we need to call our other method (see pic below for help).

Last Two Else Ifs

Before we continue we should discuss the number 4000. In roman numerals the rule before this number is such that you can never have more than 3 numerals of the same type in succession. For example 3 is III but 4 is IV. As M is the largest roman numeral what do we do when we reach 4000? There are many many debates about this. Some people believe IV is correct as a line above roman numerals (vinculum) means 1000 times that value. Others simply think the rule changes at this point and you just keep using M over and over. For the sake of simplicity we will be using the latter solution. Firstly getting an overline in Unity would require some work (potential future tutorial idea… just need to figure it out myself first :)) and secondly using roman numerals for numbers larger than 4000 will probably be rare.

At the moment all our code will do is return one roman numeral based on how big our number is. For example if we put in 11 it would return “X” however this is incorrect as it should be “XI”.

To make this work properly we are going to put all our if statements into a while loop. The condition of the loop will be while number is greater than 0.

While Loop

At the moment this is an infinite loop! Number never reduces, so it will just go on forever. So how do we fix this?

In all the if statements take the corresponding number away from number (for example, in the >=1000 if statement take 1000 away from number). This way the method will keep looping until number equals zero and adding the next roman numeral to the end of the current tally. In the case of the last if statement we make number equal to 0 (that way it will always terminate the loop).

Reducing number

This should now be complete :).

But how do we test this is working? Create a new script called Test (or open it if you made this script for Example 2 in the previous part). Attach this script to any object in the game scene and in the Start method create some new strings equal to RomanNumeralGenerator method. Pass them some numbers (I have used 44, 91, 189, 2017 and 12048 in the picture below) and then use a Debug.Log to output the answers in the console before pressing play.

Note: The answers should be XLIV, XCI, CLXXXIX, MMXVII and MMMMMMMMMMMMXLVIII for my examples.

Test Script
Results in Console

And there you have it. A working roman numeral generator script which you can use in any project (just add to the assets folder or any sub-folder located in the assets folder). Some examples of where you might use it could be to keep a points system in roman numerals, have roman numerals appear in some text in your game or name things (like our planet example last time).

Hope this short tutorial series was helpful :). Let me know in the comments.


RomanNumerals (zip file)

RomanNumerals (paste bin)



One thought on “Roman Numeral Generator Part 2

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s