markdown php将数字转换为罗马和罗马数字
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown php将数字转换为罗马和罗马数字相关的知识,希望对你有一定的参考价值。
**Number To Roman""
Source from: https://stackoverflow.com/questions/14994941/numbers-to-roman-numbers-with-php
```
/**
* @param int $number
* @return string
*/
function numberToRomanRepresentation($number) {
$map = 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);
$returnValue = '';
while ($number > 0) {
foreach ($map as $roman => $int) {
if($number >= $int) {
$number -= $int;
$returnValue .= $roman;
break;
}
}
}
return $returnValue;
}
```
**Roman to Number**
Source: https://stackoverflow.com/questions/6265596/how-to-convert-a-roman-numeral-to-integer-in-php
```
$romans = 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,
);
$roman = 'MMMCMXCIX';
$result = 0;
foreach ($romans as $key => $value) {
while (strpos($roman, $key) === 0) {
$result += $value;
$roman = substr($roman, strlen($key));
}
}
echo $result;
```
以上是关于markdown php将数字转换为罗马和罗马数字的主要内容,如果未能解决你的问题,请参考以下文章
C++:将整数转换为罗马数字的困惑
通过 XSLT 转换 XML 文件将数字转换为罗马数字
Javascript--将十进制数字转换成罗马数字显示
python 将给定的整数转换为罗马数字。
c_cpp [十进制数转罗马数字]将一个十进制数字符串转换为罗马数字字符串#字符串处理
FCC 中级算法题 罗马数字转换器