# RE: [PHP-DB] Re: Now, how about Roman Numerals?

```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```
```
So, when you go to pass info to the RomanDigit() function, the passed data
looks like this:

For reference, arguments are (\$dig, \$one, \$five, \$ten)

return(
RomanDigit(.155, 'M', '', '')
.RomanDigit(1.55, 'C', 'D', 'M')
.RomanDigit(5.5, 'X', 'L', 'C')
.RomanDigit(5, 'I', 'V', 'X')
);

When the RomanDigit Case(\$dig) control goes through and checks the \$dig part
of the passed in information, the only line that matches anything in the
case statement is the last one where \$dig is equal to 5. Using the formula
in the case statement for 5:

> >       case 5:    return "\$five";

The code will look at what the data in the argument \$five is and return the
V. Unfortunately, that's where the function stops. It doesn't do anything to
take care of the fact that the number is 155, not 5.

If you use your second example of 980, here are the results:

\$m = \$num / 1000; //\$m will be equal to .980
\$c = (\$num % 1000) / 100; //\$c will be equal to 9.8
\$x = (\$num % 100) / 10; //\$x will be equal to 80
\$i = \$num % 10; //\$i will be equal to 0

So, when you go to pass info to the RomanDigit() function, the passed data
looks like this:

For reference, arguments are (\$dig, \$one, \$five, \$ten)

return(
RomanDigit(.980, 'M', '', '')
.RomanDigit(9.8, 'C', 'D', 'M')
.RomanDigit(8, 'X', 'L', 'C')
.RomanDigit(0, 'I', 'V', 'X')
);

When the RomanDigit Case(\$dig) control goes through and checks the \$dig part
of the passed in information, the only line that matches anything in the
case statement is the last one where \$dig is equal to 8. Using the formula
in the case statement for 8:

> >       case 8:    return "\$five\$one\$one\$one";

The function will return LXXX which is the value of 8 in the tens place, or
80.

I don't know exactly how to fix it, but it would seem that the key would be
to get the function to drop anything to the right of the decimal point after
the equation fires. In this example:

RomanDigit(.980, 'M', '', '')
.RomanDigit(9.8, 'C', 'D', 'M')
.RomanDigit(8, 'X', 'L', 'C')
.RomanDigit(0, 'I', 'V', 'X')

the function would probably work because the digits are properly broken out
in the last three lines:

.RomanDigit(9.8, 'C', 'D', 'M')
.RomanDigit(8, 'X', 'L', 'C')
.RomanDigit(0, 'I', 'V', 'X')

but the first line:
.RomanDigit(9.8, 'C', 'D', 'M')

fails because the 9.8 doesn't match any of the cases. Drop the .8 and I
think it works. If you had:

RomanDigit(.980, 'M', '', '')
.RomanDigit(9, 'C', 'D', 'M')
.RomanDigit(8, 'X', 'L', 'C')
.RomanDigit(0, 'I', 'V', 'X')

Then the result would be CMLXXX which, I checked, does equal 980.

Am I on the right track here?

Rich

> -----Original Message-----
> From: David Shugarts [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, June 11, 2003 9:57 AM
> To: Hugh Bothwell; [EMAIL PROTECTED]
> Subject: Re: [PHP-DB] Re: Now, how about Roman Numerals?
>
>
> Hi, Hugh--
>
> I am very grateful for your help and very impressed with
> this, but I am not
> sure that I am able to confirm that it works and I am not
> competent to fix
> it if it does not. Perhaps you can tell where I am going wrong.
>
> I stored your script as a separate php document,
> RomanDigit.php. Here is
> what I tested it with:
>
> <?php
> include OERomanDigit.php¹;
>
> \$a=8;
> \$b=19;
> \$c=155;
> \$d=980;
>
> \$ar= IntToRoman(\$a);
> \$br= IntToRoman(\$b);
> \$cr= IntToRoman(\$c);
> \$dr= IntToRoman(\$d);
>
> echo ³
> a = \$a and ar = \$ar <br>
> b = \$b and br = \$br <br>
> c = \$c and cr = \$cr <br>
> d = \$d and dr = \$dr <br>
> ²;
>
> ?>
>
>
> And here are the results:
>
> a = 8 and ar = VIII
> b = 19 and br = IX
> c = 155 and cr = V
> d = 980 and dr = LXXX
>
> TIA--Dave Shugarts
>
>
>
> > 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) {
> >   if ((\$num < 1) || (\$num > 3999))
> >       return("No corresponding Roman number!");
> >
> >   \$m = \$num / 1000;
> >   \$c = (\$num % 1000) / 100;
> >   \$x = (\$num % 100) / 10;
> >   \$i = \$num % 10;
> >
> >   return(
> >        RomanDigit(\$m, 'M', '', '')
> >       .RomanDigit(\$c, 'C', 'D', 'M')
> >       .RomanDigit(\$x, 'X', 'L', 'C')
> >       .RomanDigit(\$i, 'I', 'V', 'X')
> >   );
> > }
>
>
> --
> 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

```