Vivid Lust Posted October 21, 2009 Share Posted October 21, 2009 how could i round to the nearest 5 or 10 with 2 decimal places? Here are some examples: 1.23 => 1.25 1.26 => 1.30 1.245 => 1.25 1.236 => 1.25 thanks! Link to comment https://forums.phpfreaks.com/topic/178519-solved-round-to-nearest-5-or-10-with-2-decimal-places/ Share on other sites More sharing options...
JAY6390 Posted October 21, 2009 Share Posted October 21, 2009 <?php /** * @author Jay Gilford * @copyright 2009 */ $vals = array(1.23,1.26,1.245,1.236); foreach($vals as &$v) { $v = ceil($v*20)/20; } echo '<pre>'.print_r($vals, true).'</pre>'; Link to comment https://forums.phpfreaks.com/topic/178519-solved-round-to-nearest-5-or-10-with-2-decimal-places/#findComment-941463 Share on other sites More sharing options...
Mchl Posted October 21, 2009 Share Posted October 21, 2009 If to nearest, then round should be used instead of ceil Link to comment https://forums.phpfreaks.com/topic/178519-solved-round-to-nearest-5-or-10-with-2-decimal-places/#findComment-941465 Share on other sites More sharing options...
JAY6390 Posted October 21, 2009 Share Posted October 21, 2009 I thought that myself, however round does not work if you do the above with it instead of ceil Link to comment https://forums.phpfreaks.com/topic/178519-solved-round-to-nearest-5-or-10-with-2-decimal-places/#findComment-941468 Share on other sites More sharing options...
Mchl Posted October 21, 2009 Share Posted October 21, 2009 It doesn't? Link to comment https://forums.phpfreaks.com/topic/178519-solved-round-to-nearest-5-or-10-with-2-decimal-places/#findComment-941472 Share on other sites More sharing options...
JAY6390 Posted October 21, 2009 Share Posted October 21, 2009 Nope, this would be the output Array ( [0] => 1.25 [1] => 1.25 [2] => 1.25 [3] => 1.25 ) Link to comment https://forums.phpfreaks.com/topic/178519-solved-round-to-nearest-5-or-10-with-2-decimal-places/#findComment-941473 Share on other sites More sharing options...
salathe Posted October 21, 2009 Share Posted October 21, 2009 To OP: Do you really want to round to the nearest 5/100ths? If so, your numbers are incorrect, 1.26 is closer to 1.25 than 1.30. Perhaps you want to round up to the next 5/100ths? If that is the case, look to JAY6390's post. (Edit: added "To OP" prefix) Link to comment https://forums.phpfreaks.com/topic/178519-solved-round-to-nearest-5-or-10-with-2-decimal-places/#findComment-941477 Share on other sites More sharing options...
Vivid Lust Posted October 21, 2009 Author Share Posted October 21, 2009 Thanks, how could i "wrap" some code around a single number to achieve this rather than having them in an array? Thanks. Link to comment https://forums.phpfreaks.com/topic/178519-solved-round-to-nearest-5-or-10-with-2-decimal-places/#findComment-941478 Share on other sites More sharing options...
Vivid Lust Posted October 21, 2009 Author Share Posted October 21, 2009 To OP: Do you really want to round to the nearest 5/100ths? If so, your numbers are incorrect, 1.26 is closer to 1.25 than 1.30. Perhaps you want to round up to the next 5/100ths? If that is the case, look to JAY6390's post. (Edit: added "To OP" prefix) My mistake! I meant to write 1.25 and not 1.30 , sorry guys Link to comment https://forums.phpfreaks.com/topic/178519-solved-round-to-nearest-5-or-10-with-2-decimal-places/#findComment-941479 Share on other sites More sharing options...
JAY6390 Posted October 21, 2009 Share Posted October 21, 2009 what do you mean by "wrap" code around it? example? Link to comment https://forums.phpfreaks.com/topic/178519-solved-round-to-nearest-5-or-10-with-2-decimal-places/#findComment-941484 Share on other sites More sharing options...
Daniel0 Posted October 21, 2009 Share Posted October 21, 2009 Thanks, how could i "wrap" some code around a single number to achieve this rather than having them in an array? Thanks. Just don't do it in an array, but do the change directly. Link to comment https://forums.phpfreaks.com/topic/178519-solved-round-to-nearest-5-or-10-with-2-decimal-places/#findComment-941485 Share on other sites More sharing options...
Vivid Lust Posted October 21, 2009 Author Share Posted October 21, 2009 more or less a function. Link to comment https://forums.phpfreaks.com/topic/178519-solved-round-to-nearest-5-or-10-with-2-decimal-places/#findComment-941486 Share on other sites More sharing options...
JAY6390 Posted October 21, 2009 Share Posted October 21, 2009 function round_it($value) { return round($value*20) / 20; } Not sure why you couldn't make one yourself, it's hardly difficult Link to comment https://forums.phpfreaks.com/topic/178519-solved-round-to-nearest-5-or-10-with-2-decimal-places/#findComment-941490 Share on other sites More sharing options...
Vivid Lust Posted October 21, 2009 Author Share Posted October 21, 2009 function round_it($value) { return round($value*20) / 20; } Not sure why you couldn't make one yourself, it's hardly difficult Because some of us are noobs. Will try it now, thanks. Link to comment https://forums.phpfreaks.com/topic/178519-solved-round-to-nearest-5-or-10-with-2-decimal-places/#findComment-941492 Share on other sites More sharing options...
JAY6390 Posted October 21, 2009 Share Posted October 21, 2009 i guess lol Link to comment https://forums.phpfreaks.com/topic/178519-solved-round-to-nearest-5-or-10-with-2-decimal-places/#findComment-941495 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.