kryppienation Posted December 12, 2008 Share Posted December 12, 2008 Howdy, I am having a problem here with rounding. I cannot seem to figure out what is going on. Here is the code. if($hardnesspicked == Stainless){ $odpicked = 5; $tpi = .953; echo 'You have picked stainless steel on the kasto saw, the time per inch will be '.$tpi.'<p>'; //let's do the math and get the PPH $mpp = $odpicked * $tpi; echo ''.$mpp.' MPP <p>'; $mpp = round($mpp, 2); echo ''.$mpp.' formatted mpp<p>'; $pph = 60 / $mpp; $pph = round($pph, 3); echo 'This Job will have a PPH = '.$pph.'.<p>'; } Here is the output: You have picked stainless steel on the kasto saw, the time per inch will be 0.953 4.765 MPP 4.76 formatted mpp This Job will have a PPH = 12.605. 4.765 should be rounding up to 4.77 I cannot figure out what i am doing wrong and why this number is not rounding to 4.77 Can anyone help me figure this out? Quote Link to comment https://forums.phpfreaks.com/topic/136609-problems-with-rounding/ Share on other sites More sharing options...
.josh Posted December 12, 2008 Share Posted December 12, 2008 $pph = round($pph, 2); Quote Link to comment https://forums.phpfreaks.com/topic/136609-problems-with-rounding/#findComment-713317 Share on other sites More sharing options...
kryppienation Posted December 12, 2008 Author Share Posted December 12, 2008 ummm.... i need pph to be a 3 point decimal... and had the mpp came out to 4.77 instead of 4.76 my pph would be correct... which should be 12.579 instead of 12.605. The problem is that the round is not taking 4.765 and turning it into 4.77. Quote Link to comment https://forums.phpfreaks.com/topic/136609-problems-with-rounding/#findComment-713322 Share on other sites More sharing options...
PFMaBiSmAd Posted December 12, 2008 Share Posted December 12, 2008 Rounding in pure math rounds to make the next higher digit even. To round 'up' requires code like the following - <?php $num = 4.765; $num = ceil($num * 100)/100; echo $num; ?> Quote Link to comment https://forums.phpfreaks.com/topic/136609-problems-with-rounding/#findComment-713329 Share on other sites More sharing options...
kryppienation Posted December 12, 2008 Author Share Posted December 12, 2008 You rock my world!! Solved. Thanks a lot PFMaBiSmAd Quote Link to comment https://forums.phpfreaks.com/topic/136609-problems-with-rounding/#findComment-713333 Share on other sites More sharing options...
PFMaBiSmAd Posted December 12, 2008 Share Posted December 12, 2008 Here is a thread with a couple of functions where you can specify the number of digits - http://www.phpfreaks.com/forums/index.php?topic=128753.0 Quote Link to comment https://forums.phpfreaks.com/topic/136609-problems-with-rounding/#findComment-713337 Share on other sites More sharing options...
kryppienation Posted December 12, 2008 Author Share Posted December 12, 2008 I just ran into a problem... if the number to be round is 4.352, this will round the number incorrectly to 4.36.... I need the number to round up if the 3rd decimal place is 5-9 and round down if the third decimal place is 0-4. Any more suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/136609-problems-with-rounding/#findComment-713348 Share on other sites More sharing options...
sasa Posted December 12, 2008 Share Posted December 12, 2008 look <?php $num = 4.765; echo $num,"<br />\n"; echo $num - 4.76; ?> Quote Link to comment https://forums.phpfreaks.com/topic/136609-problems-with-rounding/#findComment-713435 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.