note 78998 deleted from function.round by danbrown

From: Date: Sun, 17 May 2009 16:16:54 +0000
Subject: note 78998 deleted from function.round by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-154783@lists.php.net to get a copy of this message
Note Submitter: trhodes at plus dot net ---- smithaapc's trueRound() function is also broken. For example: trueRound(1.11, 3) //returns 1.1, should return 1.11 Here's a function that uses PHP's round() to calculate significant figures: <?php function sigFig($value, $sigFigs) { $exponent = floor(log10($value) + 1); $significand = $value / pow(10, $exponent); $significand = round($significand * pow(10, $sigFigs)) / pow(10, $sigFigs); $value = $significand * pow(10, $exponent); return($value); } //Examples sigFig(123.45, 6); // 123.45 (doesn't pad with zeros) sigFig(123.45, 5); // 123.45 sigFig(123.45, 4); // 123.5 sigFig(123.45, 3); // 123 sigFig(123.45, 2); // 120 (rounds to 2 figures) sigFig(123.45, 1); // 100 sigFig(123.45, 0); // 0 (!) ?>

« previous php.notes (#154783) next »