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
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 . " ’ " . $inch . ' ”';
}
?>
<?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. <?php echo convert_height($modelsRecord['height_inches']); ?> (<?php echo convert_metricheight($modelsRecord['height_inches']); ?> 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: [equinox69] rounding (or not rounding) help needed.
By Jason - September 4, 2012
I think the issue here is when you're using %12. Modulus in PHP will return an integer, which is why it's being rounded. Try this:
function convert_height($height) {
$inch = round($height * 1.00);
$ft = floor($inch / 12);
$inch = $inch - ($ft * 12);
return $ft . " ’ " . $inch . ' ”';
}
Hope this helps
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Re: [Jason] rounding (or not rounding) help needed.
By Codee - September 4, 2012
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
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?