rounding (or not rounding) help needed.

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

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

By Jason - September 4, 2012

Hi,

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

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?