note 80939 added to function.round

From: Date: Thu, 07 Feb 2008 18:30:08 +0000
Subject: note 80939 added to function.round
Groups: php.notes 
Request: Send a blank email to php-notes+get-135875@lists.php.net to get a copy of this message
Another way to code all 7 rounding methods according IEEE754 standart: <?php // We need precision parameter for ceil & floor function p_ceil( $val, $d ) { return ceil($val * pow (10, $d) )/ pow (10, $d) ; } function p_floor( $val, $d ) { return floor($val * pow (10, $d) )/ pow (10, $d) ; } // We need signum function sgn($x) { return $x ? ($x>0 ? 1 : -1) : 0; } // Now seven functions for standart rounding methods function round_down($num, $d = 0) { return sgn($num)*p_floor(abs($num), $d); } function round_up($num, $d = 0) { return sgn($num)*p_ceil(abs($num), $d); } function round_half_even($num, $d = 0) { if( round($num,$d) == round_half_down($num,$d) ) return round($num,$d); $pre_digit = round_down( $num, $d ); if( ($pre_digit/2) == round_down($pre_digit/2,$d) ) return round_down($num, $d); else return round_up($num, $d); } function round_ceiling($num, $d = 0) { return p_ceil($num, $d); } function round_floor($num, $d = 0) { return p_floor($num, $d); } function round_half_down($num, $d = 0) { return ((2*round_up($num,$d)==round_up(2*$num,$d)) ? round($num,$d) : round_down($num,$d) ); } function round_half_up($num, $d = 0) { return round($num, $d); } ?> My article (in Russian) about rounding in PHP is here: http://altsoph.ru/?p=521 ---- Server IP: 69.147.83.197 Probable Submitter: 83.167.116.19 ---- Manual Page -- http://www.php.net/manual/en/function.round.php Edit -- https://master.php.net/note/edit/80939 Del: integrated -- https://master.php.net/note/delete/80939/integrated Del: useless -- https://master.php.net/note/delete/80939/useless Del: bad code -- https://master.php.net/note/delete/80939/bad+code Del: spam -- https://master.php.net/note/delete/80939/spam Del: non-english -- https://master.php.net/note/delete/80939/non-english Del: in docs -- https://master.php.net/note/delete/80939/in+docs Del: other reasons-- https://master.php.net/note/delete/80939 Reject -- https://master.php.net/note/reject/80939 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#135875) next »