Saturday, August 30, 2008

Roman Numerals, Part 3

Back in March, we wrote a function to turn an arabic number into a Roman numeral. That function had a limitation of numbers smaller than 5000. There are ways of writing Roman numerals larger than 5000, but they are not as well accepted by purists because they evolved during later time periods. Out of respect for the purists, this new function will hinge on the old one, rather than replace it.

In the system we will be using, a bar is placed over the numeral to indicate that it is multipled by 1000:

  • V = 5000
  • X = 10,000
  • L = 50,000
  • C = 100,000
  • D = 500,000
  • M = 1 million

Since there are no Unicode characters for this purpose, we will have to cheat a little bit by using some HTML and CSS.


ASP

  1. function bigroman(ByVal arabic)
  2.     dim thousands
  3.     thousands = Array("", "M", "MM", "MMM", "M(V)", "(V)", "(V)M", "(V)MM", "(V)MMM", "M(X)")
  4.     if arabic >= 10000 then
  5.         bigroman = "(" & roman((arabic - (arabic mod 10000)) / 1000) & ")"
  6.         arabic = arabic mod 10000
  7.     end if
  8.     bigroman = bigroman & thousands((arabic - (arabic mod 1000)) / 1000)
  9.     arabic = arabic mod 1000
  10.     bigroman = bigroman & roman(arabic)
  11.     ' Convert parentheses to <span> tags.
  12.     bigroman = replace(bigroman, "(", "<span style=""text-decoration: overline"">")
  13.     bigroman = replace(bigroman, ")", "</span>")
  14. end function

PHP

  1. function bigroman($arabic)
  2. {
  3.     $thousands = Array("", "M", "MM", "MMM", "M(V)", "(V)", "(V)M", "(V)MM", "(V)MMM", "M(X)");
  4.     if ($arabic >= 10000)
  5.     {
  6.         $bigroman = "(" . roman(($arabic - fmod($arabic, 10000)) / 1000) . ")";
  7.         $arabic = fmod($arabic, 10000);
  8.     }
  9.     $bigroman .= $thousands[($arabic - fmod($arabic, 1000)) / 1000];
  10.     $arabic = fmod($arabic, 1000);
  11.     $bigroman .= roman($arabic);
  12.     // Convert parentheses to <span> tags.
  13.     $bigroman = str_replace("(", "<span style=""text-decoration: overline"">", $bigroman);
  14.     $bigroman = str_replace(")", "</span>", $bigroman);
  15.     return $bigroman;
  16. }

No comments: