rounding (or not rounding) help needed.

4 posts by 2 authors in: Forums > CMS Builder
Last Post: September 4, 2012   (RSS)

By Codee - September 4, 2012 - edited: September 4, 2012

So, I've been working on a site that has to show a professional's height in both inches and metric, with minimum amount of input choice. I set up a prepopulated drop down list field in the database called "height_inches". In this field the person selects, for example "6 feet 7 inches" for their professional's entry. Some of the prepopulated entries have the following format in the CMSB section:
48|4 feet
60|5 feet
62|5 feet 2 inches
62.5|5 feet 2 1/2 inches
78.5|6 feet 6 1/2 inches
79|6 feet 7 inches
...you get the idea. On the details .php page the following is in the header:
<?php // convert inches into feet
function convert_height($height) {
$inch = round($height * 1.00);
$ft = floor($inch / 12);
$inch = ($inch % 12);
return $ft . " &#8217; " . $inch . ' &#8221;';
}
?>
<?php // convert inches into metric
function convert_metricheight($metricheight) {
$inches = round($metricheight * 2.54);

return $inches . '';
}
?>

and in the body where the display is to occur, I use the following to show u.s. inches and then metric cm:

<?php if ($modelsRecord['height_inches']): ?>h.&nbsp;<?php echo convert_height($modelsRecord['height_inches']); ?>&nbsp;(<?php echo convert_metricheight($modelsRecord['height_inches']); ?>&nbsp;cm)<?php endif ?>

What's happening, in the us inches display, is a rounding issue that might bump the display up to the next 1/2 inch mark. So, for example when the client chooses a height of "5 feet 8 1/2 inches" the final output displays on the details php page as 5' 9" or a choice on the backend of "5 feet 9 1/2 inches" displays on the public side as 5' 10"...because of rounding and remainders.

How can I make it show correctly of the us inches and still convert to metric properly? Important considerations are:
* the site is live and has several hundred professionals listed
* data entry list choices must appear in the format "5 feet 11 1/2 inches" and not just the number "71 inches".
* only 1 choice can be made per professional so there is no duplication of entry/record allowed - which is why running a math script on the output page is necessary and automatic.
* the public display details.php page must show in the mark format for us size as 5' 11 1/2" and not be written out as "5 feet 11 1/2 inches" or "5 feet 11.5 inches".

So, obviously, making a proper correction to the conversion scripts in the head of the page is the key. It's just that I spent, literally, over 10 hours researching with trial-and-error and trying to figure it out on my own just to get it to the point where it's at. While I'm particularly stupid, I really am less than an entry level javascript programmer.

Thank you in advance to anyone, and everyone, that helps!

Re: [Jason] rounding (or not rounding) help needed.

By Codee - September 4, 2012

Hi Jason,
Thank you for the expeditious reply. Your code looked good, but I'm still seeing heights being rounded up...so what should display as 5' 8 1/2" is displaying as 5' 9", 5' 11 1/2" is displaying as 6' 0". Is there a better math function for me to use than the rounding command? I'm really stumped on this. Your help is and will be greatly appreciated.

Re: [equinox69] rounding (or not rounding) help needed.

By Codee - September 4, 2012

I think I solved it.
changing
$inch = round($height * 1);
to
$inch = round($height * 100)/100;
forces the output to still round, but to 2 decimals automatically. Since all of the measurements are on 1/2 inch increments the resulting value always ends in either .00 or .50, so rounding the second decimal always works. I've tested it and it looks right so far. Anyone see a flaw with this?