RE: [PHP-DB] Re: Now, how about Roman Numerals?
| From: | Hutchins, Richard | Date: | Wed, 11 Jun 2003 15:25:24 +0000 |
| Subject: | RE: [PHP-DB] Re: Now, how about Roman Numerals? | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-28542@lists.php.net to get a copy of this message | ||
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: [PHP-DB] Re: Now, how about Roman Numerals?
>
>
> "Richard Hutchins" <Richard.Hutchins@Getingeusa.com> wrote in message
> 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 => ".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
>