You are here

function _tableofcontents_roman in Table of Contents 7.2

Same name and namespace in other branches
  1. 6.3 tableofcontents.pages.inc \_tableofcontents_roman()
  2. 7 tableofcontents.pages.inc \_tableofcontents_roman()
1 call to _tableofcontents_roman()
_tableofcontents_convert_number in ./tableofcontents.themes.inc
Transform a decimal number in one of:

File

./tableofcontents.themes.inc, line 128
Table of Contents - Versatile system for generating Tables of Contents for fields - themes.

Code

function _tableofcontents_roman($arabic) {

  //static $fractions = Array("", "�", "��", "���", "����", "�����", "S", "S�", "S��", "S���", "S����", "S�����", "I");
  static $ones = array(
    "",
    "I",
    "II",
    "III",
    "IV",
    "V",
    "VI",
    "VII",
    "VIII",
    "IX",
  );
  static $tens = array(
    "",
    "X",
    "XX",
    "XXX",
    "XL",
    "L",
    "LX",
    "LXX",
    "LXXX",
    "XC",
  );
  static $hundreds = array(
    "",
    "C",
    "CC",
    "CCC",
    "CD",
    "D",
    "DC",
    "DCC",
    "DCCC",
    "CM",
  );
  static $thousands = array(
    "",
    "M",
    "MM",
    "MMM",
    "MMMM",
  );
  if ($arabic > 4999) {

    // For large numbers (five thousand and above), a bar is placed above a base numeral to indicate multiplication by 1000.
    // Since it is not possible to illustrate this in plain ASCII, this function will refuse to convert numbers above 4999.

    //die("Cannot represent numbers larger than 4999 in plain ASCII.");
    return $arabic;
  }
  elseif ($arabic == 0) {

    // About 725, Bede or one of his colleagues used the letter N, the initial of nullae,
    // in a table of epacts, all written in Roman numerals, to indicate zero.
    return "N";
  }
  else {

    // Handle fractions that will round up to 1.

    //if (round(fmod($arabic, 1) * 12) == 12) {

    //  $arabic = round($arabic);

    //}

    // With special cases out of the way, we can proceed.
    // NOTE: modulous operator (%) only supports integers, so fmod() had to be used instead to support floating point.
    $m = fmod($arabic, 1000);
    $roman = $thousands[($arabic - $m) / 1000];
    $arabic = $m;
    $m = fmod($arabic, 100);
    $roman .= $hundreds[($arabic - $m) / 100];
    $arabic = $m;
    $m = fmod($arabic, 10);
    $roman .= $tens[($arabic - $m) / 10];
    $arabic = $m;
    $m = fmod($arabic, 1);
    $roman .= $ones[($arabic - $m) / 1];

    // Handling for fractions.

    //$arabic = $m;

    //if ($arabic > 0) {

    //  $roman .= $fractions[round($arabic * 12)];

    //}
    return $roman;
  }
}