jaymc Posted December 22, 2008 Share Posted December 22, 2008 Do you know how I can always round down to the nearest 20, starting from 0,20,40,60 and so on Example 0 rounds to 0 7 rounds to 0 19 rounds to 0 20 rounds to 0 21 rounds to 20 36 rounds to 20 44 rounds to 40 88 rounds to 80 And so on.. Quote Link to comment https://forums.phpfreaks.com/topic/138048-solved-always-round-down-in-20s/ Share on other sites More sharing options...
DeanWhitehouse Posted December 22, 2008 Share Posted December 22, 2008 I don't know any pre-made functions to do this, but you could easily write your own. Quote Link to comment https://forums.phpfreaks.com/topic/138048-solved-always-round-down-in-20s/#findComment-721526 Share on other sites More sharing options...
.josh Posted December 22, 2008 Share Posted December 22, 2008 $num = floor($num/20) * 20; Quote Link to comment https://forums.phpfreaks.com/topic/138048-solved-always-round-down-in-20s/#findComment-721529 Share on other sites More sharing options...
Mark Baker Posted December 22, 2008 Share Posted December 22, 2008 Surely 20 should round down to 20, not to 0 (or was that just a typo) Quote Link to comment https://forums.phpfreaks.com/topic/138048-solved-always-round-down-in-20s/#findComment-721531 Share on other sites More sharing options...
jaymc Posted December 22, 2008 Author Share Posted December 22, 2008 $num = floor($num/20) * 20; Ah yes, thats how I used to do it That works apart from when $num = any of these 20,40,60,80 I need it like this Example 20 rounds to 0 40 rounds to 20 60 rounds to 40 80 rounds to 60 Any ideas to get around that issue? Quote Link to comment https://forums.phpfreaks.com/topic/138048-solved-always-round-down-in-20s/#findComment-721533 Share on other sites More sharing options...
wildteen88 Posted December 22, 2008 Share Posted December 22, 2008 $nums = array(0, 1, 7, 20, 40, 55); foreach($nums as $key => $num) { // check that numb is dividable by 20 if(num >= 20 && ($num%20 == 0)) $num -= 20; else $num = floor($num/20) * 20; $rounded[] = $num; } echo '<pre>' . print_r($rounded, true) . '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/138048-solved-always-round-down-in-20s/#findComment-721540 Share on other sites More sharing options...
jaymc Posted December 22, 2008 Author Share Posted December 22, 2008 $nums = array(0, 1, 7, 20, 40, 55); foreach($nums as $key => $num) { // check that numb is dividable by 20 if(num >= 20 && ($num%20 == 0)) $num -= 20; else $num = floor($num/20) * 20; $rounded[] = $num; } echo '<pre>' . print_r($rounded, true) . '</pre>'; That.. didnt work? $nums = array(0, 1, 7, 20, 40, 55, 60, 80, 81); produced this.. Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 20 [4] => 40 [5] => 40 [6] => 60 [7] => 80 [8] => 80 ) Quote Link to comment https://forums.phpfreaks.com/topic/138048-solved-always-round-down-in-20s/#findComment-721547 Share on other sites More sharing options...
.josh Posted December 22, 2008 Share Posted December 22, 2008 $num = floor($num/20) * 20; Ah yes, thats how I used to do it That works apart from when $num = any of these 20,40,60,80 I need it like this Example 20 rounds to 0 40 rounds to 20 60 rounds to 40 80 rounds to 60 Any ideas to get around that issue? Hey it's your project, so you must know what you're doing. All I'm gonna say is...does 0 get rounded down to -20? So why would 20 get rounded down to 0, 40 to 20, etc...? Not necessarily arguing; just curious. Quote Link to comment https://forums.phpfreaks.com/topic/138048-solved-always-round-down-in-20s/#findComment-721552 Share on other sites More sharing options...
.josh Posted December 22, 2008 Share Posted December 22, 2008 Wildteen's code forgot $ for $num if($num >= 20 && ($num%20 == 0)) Quote Link to comment https://forums.phpfreaks.com/topic/138048-solved-always-round-down-in-20s/#findComment-721556 Share on other sites More sharing options...
jaymc Posted December 22, 2008 Author Share Posted December 22, 2008 Wildteen's code forgot $ for $num if($num >= 20 && ($num%20 == 0)) Yeh just spotted, could not work out why it wasnt working! Cheers guys. Quote Link to comment https://forums.phpfreaks.com/topic/138048-solved-always-round-down-in-20s/#findComment-721559 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.