I think Roman Numerals are particuarly attractive when used well on the web.
This website uses Roman Numerals for the copyright date. Of course, this is easily implemented in PHP with the code below:
<?php function numberToRoman($num) { // Make sure that we only use the integer portion of the value $n = intval($num); $result = ''; // Declare a lookup array that we will use to traverse the number: $lookup = 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); foreach ($lookup as $roman => $value) { // Determine the number of matches $matches = intval($n / $value); // Store that many characters $result .= str_repeat($roman, $matches); // Substract that from the number $n = $n % $value; } // The Roman numeral should be built, return it return $result; } echo(numberToRoman(date('Y'))) ?>
In ColdFusion, though, I have not been able to find a ‘numberToRoman’ function anywhere, so had to build my own.
Solution
The ‘RomanNumeralsFormat’ function utilises another function I created called ‘RoundDown’. First, we declare the latter:
<cfscript> function RoundDown(numberIn,roundBy){ var resultantNumber = 0; if( (numberIn MOD roundBy) NEQ 0 ) { resultantNumber = numberIn - (numberIn MOD roundBy); }else{ resultantNumber = numberIn; } return resultantNumber; } </cfscript>
Then RomanNumeralsFormat is ready to be declared:
<cffunction name="RomanNumeralsFormat" returntype="string" displayname="Convert to roman numerals"> <cfargument name="number" type="numeric" required="yes" displayname="The required number to pass in"> <cfset numberOrig = number> <cfset number = round(number)> <cfset onesChrs = "I,II,III,IV,V,VI,VII,VIII,IX"> <cfset tensChrs = "X,XX,XXX,XL,L,LX,LXX,LXXX,XC"> <cfset hundredsChrs = "C,CC,CCC,CD,D,DC,DCC,DCCC,CM"> <cfset thousandsChrs = "M,MM,MMM,MMMM"> <cfif number GT 4999> <cfreturn numberOrig> <cfthrow message="Cannot represent numbers larger than 4999 in plain ASCII."> <cfelseif number EQ 0> <!--- (nullae) ---> <cfreturn "N"> <cfelse> <cfset result = ""> <cfset thousandsCount = RoundDown(number,1000,'down') / 1000> <cfif thousandsCount gt 0> <cfset result = listGetAt(thousandsChrs,thousandsCount)> </cfif> <cfset hundredsCount = RoundDown(number-(1000*thousandsCount),100,'down') / 100> <cfif hundredsCount gt 0> <cfset result = result & listGetAt(hundredsChrs,hundredsCount)> </cfif> <cfset tensCount = RoundDown(number-(1000*thousandsCount)-(100*hundredsCount),10,'down') / 10> <cfif tensCount gt 0> <cfset result = result & listGetAt(tensChrs,tensCount)> </cfif> <cfset onesCount = RoundDown(number-(1000*thousandsCount)-(100*hundredsCount)-(10*tensCount),1,'down')> <cfif onesCount gt 0> <cfset result = result & listGetAt(onesChrs,onesCount)> </cfif> <cfreturn result> </cfif> </cffunction>
We call the function like this:
<cfdump var="#RomanNumeralsFormat('999')#"> <!--- Produces: CMXCIX ---> <cfdump var="#RomanNumeralsFormat('2009')#"> <!--- Produces: MMIX ---> <cfdump var="#RomanNumeralsFormat('2008.9')#"> <!--- Rounds to 2009 and Produces: MMIX ---> <cfdump var="#RomanNumeralsFormat('11.11')#"> <!--- Rounds to 11 and Produces: XI ---> <cfdump var="#RomanNumeralsFormat('11.99')#"> <!--- Rounds to 12 and Produces: XII --->
I hope you find this post interesting and the function can be of use to you.





