?
Solved

String operations

Posted on 2007-09-27
7
Medium Priority
?
871 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:matthew016
  • 4
  • 3
7 Comments
 
LVL 13

Accepted Solution

by:
MasonWolf earned 1000 total points
ID: 19976768
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
 
LVL 1

Assisted Solution

by:kennethw1
kennethw1 earned 1000 total points
ID: 19977514
<?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
 
LVL 13

Expert Comment

by:MasonWolf
ID: 19978108
A word of advice kennethw1:

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Expert Comment

by:kennethw1
ID: 19980572
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
 
LVL 13

Expert Comment

by:MasonWolf
ID: 19980688
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
 
LVL 13

Expert Comment

by:MasonWolf
ID: 19986490
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
 
LVL 1

Expert Comment

by:kennethw1
ID: 19986818
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month15 days, 12 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question