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 13 XIII 14 XIV 15 XV 20 XX 30 XXX 40 XL 50 L 60 LX 70 LXX 80 LXXX 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.

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

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

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”.

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

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

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).

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.

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).

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.