Re: Formatting Decimals
| From: | Paul M Foster | Date: | Tue, 12 Jan 2010 04:47:46 +0000 |
| Subject: | Re: Formatting Decimals | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-301009@lists.php.net to get a copy of this message | ||
On Mon, Jan 11, 2010 at 04:56:03PM -0500, tedd wrote:
>
> --Rick:
>
> The above described rounding algorithm introduces more bias than
> simply using PHP's round() function, which always rounds down. IMO,
> modifying rounding is not worth the effort.
>
> The "best" rounding algorithm is to look at the last digit and do this:
>
> 0 -- no rounding needed.
> 1-4 round down.
> 6-9 round up.
>
> In the case of 5, then look to the number that precedes it -- if it
> is even, then round up and if it is odd, then round down -- or vise
> versa, it doesn't make any difference as long as you are consistent.
>
> Here are some examples:
>
> 122.4 <-- round down (122)
> 122.6 <-- round up (123)
> 122.5 <-- round up (123)
>
> 123.4 <-- round down (123)
> 123.6 <-- round up (124)
> 123.5 <-- round down (123)
>
> There are people who claim that there's no difference, or are at odds
> with this method, but they simply have not investigated the problem
> sufficiently to see the bias that rounding up/down causes. However,
> that difference is very insignificant and can only be seen after tens
> of thousands iterations. PHP's rounding function is quite sufficient.
This is called (among other things) "banker's rounding". But PHP's
round() function won't do banker's rounding, as far as I know.
Paul
--
Paul M. Foster