johnsmith153 Posted November 13, 2009 Share Posted November 13, 2009 I need a formula for rounding a number to 2 decimal places, but the last number must be a 5 or 0 (i.e. 1 dp if it is 0) so: 1.4798 = 1.5 5.4567 = 5.45 1.2356 = 1.25 1.2011 = 1.2 1.1945 = 1.2 Link to comment https://forums.phpfreaks.com/topic/181423-php-number-rounding/ Share on other sites More sharing options...
knsito Posted November 13, 2009 Share Posted November 13, 2009 I need a formula for rounding a number to 2 decimal places, but the last number must be a 5 or 0 (i.e. 1 dp if it is 0) so: 1.4798 = 1.5 5.4567 = 5.45 1.2356 = 1.25 1.2011 = 1.2 1.1945 = 1.2 function roundTo($number, $to){ return number_format(round($number/$to, 0)* $to, 2); } echo roundTo($number,.05); taken from http://www.php.net/manual/en/function.round.php#93747 Link to comment https://forums.phpfreaks.com/topic/181423-php-number-rounding/#findComment-957058 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.