note 38928 modified in function.round by danbrown
| From: | danbrown@php.net | Date: | Sun, 17 May 2009 16:24:20 +0000 |
| Subject: | note 38928 modified in function.round by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-154819@lists.php.net to get a copy of this message | ||
To round any number to a given number of significant digits, use log10 to find out its magnitude:
<?php round($n, ceil(0 - log10($n)) + $sigdigits); ?>
Or when you have to display a per-unit price which may work out to be less than a few
cents/pence/yen you can use:
<?php
// $exp = currency decimal places - 0 for Yen/Won, 2 for most others
$dp = ceil(0 - log10($n)) + $sigdigits;
$display = number_format($amount, ($exp>$dp)?$exp:$dp);
?>
This always displays at least the number of decimal places required by the currency, but more if
displaying the unit price with precision requires it - eg: 'English proofreading from $0.0068
per word', 'English beer from $6.80 per pint'.
--was--
To round any number to a given number of significant digits, use log10 to find out its magnitude:
round($n, ceil(0 - log10($n)) + $sigdigits);
Or when you have to display a per-unit price which may work out to be less than a few
cents/pence/yen you can use:
// $exp = currency decimal places - 0 for Yen/Won, 2 for most others
$dp = ceil(0 - log10($n)) + $sigdigits;
$display = number_format($amount, ($exp>$dp)?$exp:$dp);
This always displays at least the number of decimal places required by the currency, but more if
displaying the unit price with precision requires it - eg: 'English proofreading from $0.0068
per word', 'English beer from $6.80 per pint'.
http://php.net/manual/en/function.round.php