Home  >  Article  >  Backend Development  >  How to convert integer numbers to Roman numerals in PHP? (code example)

How to convert integer numbers to Roman numerals in PHP? (code example)

青灯夜游
青灯夜游Original
2019-03-16 17:25:323332browse

How to convert an integer number to Roman numerals for display? The following article will show you how to use PHP to convert integer numbers to Roman numerals. I hope it will be helpful to you.

How to convert integer numbers to Roman numerals in PHP? (code example)

Method 1: Custom function

We can manually write a function to achieve this function , this function can take a number as the first argument, convert it to Roman and return it.

Note: Most algorithms only work in the range of 1-4999, if extremely large numbers are used, the script will fail.

Implementation code:

<?php
header("content-type:text/html;charset=utf-8");
//将数字转换为罗马表示形式
function numberToRoman($num)  
{ 
    // Be sure to convert the given parameter into an integer
    $n = intval($num);
    $result = &#39;&#39;; 
 
    // Declare a lookup array that we will use to traverse the number: 
    $lookup = array(
        &#39;M&#39; => 1000, &#39;CM&#39; => 900, &#39;D&#39; => 500, &#39;CD&#39; => 400, 
        &#39;C&#39; => 100, &#39;XC&#39; => 90, &#39;L&#39; => 50, &#39;XL&#39; => 40, 
        &#39;X&#39; => 10, &#39;IX&#39; => 9, &#39;V&#39; => 5, &#39;IV&#39; => 4, &#39;I&#39; => 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; 
} 
echo &#39;整数数字转换为罗马数字:<br><br>&#39;;
// VIII
echo &#39;数字8:&#39;.numberToRoman(8).&#39;<br>&#39;;
// CXXIII
echo &#39;数字123:&#39;.numberToRoman(123).&#39;<br>&#39;;
// MMCCCLV
echo &#39;数字2355:&#39;.numberToRoman(2355).&#39;<br>&#39;;
// MMMMCMXCIX
echo &#39;数字4999:&#39;.numberToRoman(4999).&#39;<br>&#39;;
?>

Output:

How to convert integer numbers to Roman numerals in PHP? (code example)

##Method 2: Use Romans library

Romans library is a very simple PHP Roman numeral library that allows you to convert integers to their Roman representation and vice versa.

Note: If you do not have this library, you need to install it first; after installing the Romans library, you can use its namespace and use functions that can help convert numbers.

The Romans library contains a pair of simple filters for converting a string with Roman numerals into an int representing the input as decimal, and converting a decimal int into a string with Roman numerals as the result.

1. Convert integers to Roman numerals

To convert integers to Roman representation, you need to use the IntToRoman class, create an instance and call the filter method from it. This method takes a number as the first parameter and returns a string with Roman numerals:

<?php
use Romans\Filter\IntToRoman;

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

Output:

MCMXCIX

2. Convert Roman numerals to integers

To convert Roman numerals to integer representation, you need to use the RomanToInt class, create an instance and call the filter method from it. This method takes a string using Roman numerals as the first parameter and returns an integer with a numeric value:

<?php
use Romans\Filter\RomanToInt;

$filter = new RomanToInt();
$result = $filter->filter(&#39;MCMXCIX&#39;);
echo $result;
?>

Output:

1999

Recommended related video tutorials: "

PHP Tutorial

The above is the entire content of this article, I hope it will be helpful to everyone’s study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to convert integer numbers to Roman numerals in PHP? (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn