RE: Re: Now, how about Roman Numerals?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Now that's sweet, Hugh. Just looked at it in the browser. Nice conversion.
You should publish this as a class so it's available from this point
forward.

Rich

> -----Original Message-----
> From: Hugh Bothwell [mailto:hugh_bothwell@hotmail.com]
> Sent: Wednesday, June 11, 2003 11:19 AM
> To: php-db@lists.php.net
> Subject: Re:  Re: Now, how about Roman Numerals?
> 
> 
> "Richard Hutchins" <Richard.Hutchins@Getingeusa.com> wrote in message
> 1EA7D3AE70ACD511BE6D006097A78C1E033C8F9A@USROCEXC">news:1EA7D3AE70ACD511BE6D006097A78C1E033C8F9A@USROCEXC...
> > 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     hugh_bothwell@hotmail.com     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
> 

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


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux