Learn how to convert an integer to its roman representation and viceversa.

How to convert an integer number to a roman number in PHP

There are a lot of things that you need to think off when you try to convert a number into its roman representation, so let's think about the basics, a smaller number in front of a larger number means subtraction, all else means addition. For example, IV means 4, VI means 6. You would not put more than one smaller number in front of a larger number to subtract. For example, IIV would not mean 3, so you must separate ones, tens, hundreds, and thousands as separate items. That means that 99 is XCIX, 90 + 9, but never should be written as IC. Similarly, 999 cannot be IM and 1999 cannot be MIM. The way to follow this logic in programming (at least in the way of number to roman), can be easily understood, however the way from a roman number to a numeric values is more difficult, so the explanation won't be mentioned in this article (however we'll do it with a library).

In this article, we'll show you how to convert a numeric value into its roman representation and viceversa if you use a library for it.

A. Using a single function

If you are unable to use a library or you simply want to keep it simple, add the following function to your collection. This functions converts your number (first argument) to its roman representation:

Note

Most of the algorithms only work in the range of 1 - 4999, so with a major number the script will fail.

<?php

/**
 * Converts a number to its roman presentation.
 **/ 
function numberToRoman($num)  
{ 
    // Be sure to convert the given parameter into an integer
    $n = intval($num);
    $result = ''; 
 
    // Declare a lookup array that we will use to traverse the number: 
    $lookup = array(
        '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
    ); 
 
    foreach ($lookup as $roman => $value)  
    {
        // Look for number of matches
        $matches = intval($n / $value); 
 
        // Concatenate characters
        $result .= str_repeat($roman, $matches); 
 
        // Substract that from the number 
        $n = $n % $value; 
    } 

    return $result; 
} 

By simply including this function in your project you will be able to use it as follows:

<?php

// VIII
echo numberToRoman(8);

// CXXIII
echo numberToRoman(123);

// MMCCCLV
echo numberToRoman(2355);

// MMMMCMXCIX
echo numberToRoman(4999);

And we didn't need to include a library for such a feature.

B. Using a library

If you want to use a library instead of writing your own function, we recommend you to use the Romans library, a very Simple PHP Roman Numerals Library that allow you to convert integers to its roman representation and viceversa. The preferred way to install the library is using composer, so you can run the following command

composer require wandersonwhcr/romans

After installing the library you will be able to use its namespace and use the functions that will help you to convert the numbers. For more information about this library, please visit the official repository at Github here.

The library includes a simple couple of filters to convert a string with Roman number to an int that represents the input as decimal, and decimal int to a string with Roman number as result.

Number to Roman

To convert an integer to its roman representation, use the IntToRoman class, create an instance and call the filter method from it. This method expects as first argument the number and returns a string with the roman number:

use Romans\Filter\IntToRoman;

$filter = new IntToRoman();
$result = $filter->filter(1999);

// MCMXCIX
echo $result;

Roman to Number

To convert a roman number to its numeric representation, use the RomanToInt class, create an instance and call the filter method from it. This method expects as first argument the string with the roman number and returns an integer with the numeric value:

use Romans\Filter\RomanToInt;

$filter = new RomanToInt();
$result = $filter->filter('MCMXCIX');

// 1999
echo $result;

Happy coding !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors