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
function bigroman(ByVal arabic)dim thousandsthousands = Array("", "M", "MM", "MMM", "M(V)", "(V)", "(V)M", "(V)MM", "(V)MMM", "M(X)")if arabic >= 10000 thenbigroman = "(" & roman((arabic - (arabic mod 10000)) / 1000) & ")"arabic = arabic mod 10000end ifbigroman = bigroman & thousands((arabic - (arabic mod 1000)) / 1000)arabic = arabic mod 1000bigroman = bigroman & roman(arabic)' Convert parentheses to <span> tags.bigroman = replace(bigroman, "(", "<span style=""text-decoration: overline"">")bigroman = replace(bigroman, ")", "</span>")end function
PHP
function bigroman($arabic){$thousands = Array("", "M", "MM", "MMM", "M(V)", "(V)", "(V)M", "(V)MM", "(V)MMM", "M(X)");if ($arabic >= 10000){$bigroman = "(" . roman(($arabic - fmod($arabic, 10000)) / 1000) . ")";$arabic = fmod($arabic, 10000);}$bigroman .= $thousands[($arabic - fmod($arabic, 1000)) / 1000];$arabic = fmod($arabic, 1000);$bigroman .= roman($arabic);// Convert parentheses to <span> tags.$bigroman = str_replace("(", "<span style=""text-decoration: overline"">", $bigroman);$bigroman = str_replace(")", "</span>", $bigroman);return $bigroman;}

No comments:
Post a Comment