# String operations

Hi,

I want to convert from centimeters to feet

<?php
for(\$i=130; \$i<=220; ++\$i)
echo '<option value="' . \$i . '">' . \$i . ' cm ( ' . \$i*0.0328 . ' ft)';
?>

The problem is that I have results like :

4.2968 ft

It should be : 4'3'' in this case (notice the rounding also).

Thank you for any help on formatting the correct output.
LVL 9
###### Who is Participating?

Commented:
1 inch = 2.54 cm by definition.

Since it's possible to be exact in the equation itself, I think you should, but then again, I was a high-school science teacher once upon a time, so I probably have stronger feelings about it than most.

Anyway, here ya go:
<?php
for(\$i=130; \$i<=220; ++\$i)
echo '<option value="' . \$i . '">' . \$i . ' cm ( ' . floor(round(\$i /2.54)/12) . "'" . round(\$i / 2.54)%12 .'" ft)';
?>
0

Commented:
<?php
for(\$i=130; \$i<=220; ++\$i)
\$value = \$i * 0.0328;
\$value = round(\$value, 1);
\$value = str_replace('.', '\'', \$value)."\"";
echo "<option value=\"\$i\">\$icm (\$value ft)</option>";
?>
Tested this. This will help you get the single quotation and the double quotation mark at your ft measurement.
0

Commented:

If you see that another expert has already posted a solution, then before testing and posting your own, go ahead and test theirs. If it works, then unless your solution improves on the answer in some way, keep it to yourself. If your answer is better in some way, (especially if the previous answer is broken somehow) then that's the time to post it.
0

Commented:
Well im using a different method. And its overall cleaner, im sure mathew would love have some clean code. Not trying to steal any credits there.
0

Commented:
Ok forget it. You had a good reason for thinking your solution was better. Good luck on EE and welcome to the community. We're generally a fun group.
0

Commented:
What bothered me most about kennethw1's answer is that it wasn't technically correct. It only works if 1 foot = 10 inches. I'm annoyed that we're sharing points when I've got the only correct solution and I was also first to post it.
0

Commented:
You can have all my points if thats what you cared about. I came to help not to bother about any points. Formula i use was given by the author. If theres another mathematical formula to convert inches, im sure he can just swap the value.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.