jaymc Posted February 7, 2007 Share Posted February 7, 2007 I can I round up and down to the nearest base 25 e.g 12 = 0 16 = 25 19 = 25 29 = 25 44 = 50 Quote Link to comment Share on other sites More sharing options...
effigy Posted February 7, 2007 Share Posted February 7, 2007 This is not the most efficient method because you're recalculating the nearest 25 every time. Optimizing this depends on how often you're running it, and the range of numbers you're working with. <pre> <?php $numbers = range(0,100); foreach ($numbers as $number) { printf('%2d = ', $number); if ($number % 25 <= 12) { while ($number % 25) { --$number; } } else { while ($number % 25) { ++$number; } } echo $number, '<br>'; } ?> </pre> Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted February 7, 2007 Share Posted February 7, 2007 <?php function roundByFactor($input, $factor) { $input = round($input); $modulus = $input % $factor; if($modulus >= ($factor / 2)) { $input = roundByFactor($input + $modulus, $factor); } else { $input = $input - $modulus; } return $input; } echo roundByFactor(32, 25); // outputs 25 ?> unfortunately it doesn't work right for negative numbers. edit: sorry, fixed it, should work for decimals now. Quote Link to comment Share on other sites More sharing options...
Hypnos Posted February 7, 2007 Share Posted February 7, 2007 I wrote this function before the post above was made. But I thought I would still post it. <?php function roundbase25($number) { $number = round($number / 25) * 25; return $number; } It seems to work with everything I've thrown at it. This... print "3 " . roundbase25(3) . "\n"; print "20 " . roundbase25(20) . "\n"; print "226 " . roundbase25(226) . "\n"; will output this... 3 0 20 25 226 225 Quote Link to comment Share on other sites More sharing options...
effigy Posted February 7, 2007 Share Posted February 7, 2007 genericnumber1: There are some problems with your code when provided a larger test range. Hypnos: Well done. Quote Link to comment Share on other sites More sharing options...
jaymc Posted February 7, 2007 Author Share Posted February 7, 2007 That works perfect, however, I forgot to add, I wont to to always round DOWN to the nearest 25 17 = 0 226 = 225 274 = 250 Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 7, 2007 Share Posted February 7, 2007 Work with Hypnos's function (damn pretty function there): <?php function roundbase25($number) { $number = round($number / 25) * 25; return $number; } $number = 274; $round = roundbase25($number); if($round > $number) { $round -= 25; } ?> Quote Link to comment Share on other sites More sharing options...
Hypnos Posted February 7, 2007 Share Posted February 7, 2007 Try this: <?php function roundbase25down($number) { $number = floor($number / 25) * 25; return $number; } print "3 " . roundbase25down(3) . "\n"; print "20 " . roundbase25down(20) . "\n"; print "226 " . roundbase25down(226) . "\n"; 3 0 20 0 226 225 Quote Link to comment Share on other sites More sharing options...
jaymc Posted February 10, 2007 Author Share Posted February 10, 2007 Worked perfect Thanks Quote Link to comment 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.