Posted By

Scooter on 08/30/08


Tagged

roman numerals


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

wizard04


BigRoman


 / Published in: PHP
 

URL: http://reusablecode.blogspot.com/2008/08/roman-numerals-part-3.html

Handles arabic numbers larger than 4999. Requires my roman() function.

  1. <?php
  2. /*
  3.   Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved.
  4.  
  5.   This work is licensed under the Creative Commons Attribution License. To view
  6.   a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
  7.   send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
  8.   94305, USA.
  9.   */
  10.  
  11. // Convert Arabic numerals into Roman numerals.
  12. // REQUIRES: roman()
  13. function bigroman($arabic)
  14. {
  15. $thousands = Array("", "M", "MM", "MMM", "M(V)", "(V)", "(V)M", "(V)MM", "(V)MMM", "M(X)");
  16.  
  17. if ($arabic >= 10000)
  18. {
  19. $bigroman = "(" . roman(($arabic - fmod($arabic, 10000)) / 1000) . ")";
  20. $arabic = fmod($arabic, 10000);
  21. }
  22.  
  23. $bigroman .= $thousands[($arabic - fmod($arabic, 1000)) / 1000];
  24. $arabic = fmod($arabic, 1000);
  25. $bigroman .= roman($arabic);
  26.  
  27. // Convert parentheses to <span> tags.
  28. $bigroman = str_replace("(", "<span style=""text-decoration: overline"">", $bigroman);
  29. $bigroman = str_replace(")", "</span>", $bigroman);
  30.  
  31. return $bigroman;
  32. }
  33. ?>

Report this snippet  

You need to login to post a comment.