note 24379 added to function.round
| From: | php at silisoftware dot com | Date: | Wed, 14 Aug 2002 18:15:13 +0000 |
| Subject: | note 24379 added to function.round | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-35017@lists.php.net to get a copy of this message | ||
Here's a function to round to an arbitary number of significant digits. Don't confuse it
with rounding to a negative precision - that counts back from the decimal point, this function
counts forward from the Most Significant Digit.
ex:
round(1241757, -3); // 1242000
RoundSigDigs(1241757, 3); // 1240000
Works on negative numbers too. $sigdigs should be >= 0
function RoundSigDigs($number, $sigdigs) {
$multiplier = 1;
while ($number < 0.1) {
$number *= 10;
$multiplier /= 10;
}
while ($number >= 1) {
$number /= 10;
$multiplier *= 10;
}
return round($number, $sigdigs) * $multiplier;
}
--
http://www.php.net/manual/en/function.round.php
http://master.php.net/manage/user-notes.php?action=edit+24379
http://master.php.net/manage/user-notes.php?action=delete+24379
http://master.php.net/manage/user-notes.php?action=reject+24379