Hi, Hugh--

Thanks for both scripts! They are absolutely what I needed for my relatively
simple case and they now work great.

While I was thrashing around in my search for a conversion script, I found a
number of items that were out there in the realm of Javascript and
postgresql, etc. They were inscrutable to me, but I believe that some of
them were coming to grips with the finer points of roman numeral conversion.
While I cannot be a good judge, it sure seemed to me that this fellow had
covered all bases:

    http://www.onlineconversion.com/roman_numerals_advanced.htm

Hope this is of some contribution to the general good.

I am amazed now to think what certain database software that blithely
converts roman-int-roman is actually doing behind the scenes!

--Dave Shugarts



> "Richard Hutchins" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
>> Here's a rundown of what the script is doing based on your input:
>> 
>> If you pass in the number 155, here are the calculations:
>> $m = $num / 1000; //$m will be equal to .155
>> $c = ($num % 1000) / 100; //$c will be equal to 1.55
>> $x = ($num % 100) / 10; //$x will be equal to 5.5
>> $i = $num % 10; //$i will be equal to 5
>> [snip]
> 
> Yes, that's exactly the problem... I assumed
> integer-only input and casting.  Here is a fixed
> (tested) version:
> 
> 
> <?php
> 
> function RomanDigit($dig, $one, $five, $ten) {
> switch($dig) {
>     case 0:    return "";
>     case 1:    return "$one";
>     case 2:    return "$one$one";
>     case 3:    return "$one$one$one";
>     case 4:    return "$one$five";
>     case 5:    return "$five";
>     case 6:    return "$five$one";
>     case 7:    return "$five$one$one";
>     case 8:    return "$five$one$one$one";
>     case 9:    return "$one$ten";
> }
> }
> 
> function IntToRoman($num) {
> $num = (int) $num;
> if (($num < 1) || ($num > 3999))
>     return("No corresponding Roman number!");
> 
> $m = (int) ($num * 0.001);          $num -= $m*1000;
> $c = (int) ($num * 0.01);           $num -= $c*100;
> $x = (int) ($num * 0.1);            $num -= $x*10;
> $i = (int) ($num);
> 
> // echo "m = $m, c = $c, x = $x, i = $i   ";
> 
> return(
>      RomanDigit($m, 'M', '', '')
>     . RomanDigit($c, 'C', 'D', 'M')
>     . RomanDigit($x, 'X', 'L', 'C')
>     . RomanDigit($i, 'I', 'V', 'X')
> );
> }
> 
> ?>
> 
> 
> and my test script:
> 
> <?php
> 
> include("to_roman.php");
> 
> $test = array( 8, 19, 155, 980, 9.8, -3, 3999, 4000, "abc", "", array() );
> 
> foreach($test as $num)
>   echo "$num =&gt; ".IntToRoman($num)."<br/>";
> 
> ?>
> 
> 
> --
> Hugh Bothwell     [EMAIL PROTECTED]     Kingston ON Canada
> v3.1 GCS/E/AT d- s+: a- C+++ L++>+++$ P+ E- W+++$ N++ K? w++ M PS+
> PE++ Y+ PGP+ t-- 5++ !X R+ tv b++++ DI+++ D-(++) G+ e(++) h-- r- y+
> 
> 
> 


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to