RE: round to nearest 500?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Ok, screw work.. it's snowing out anyway (not that that makes a real difference to doing PHP work inside), curiosity got the better of me.

btw.. the "banker" rounding code here was pulled from the round() manual page.  It's not what I read before, but it's the same concept:

function bankers_round ($moneyfloat = null)
{
   $money_str = sprintf("%01.3f", round($moneyfloat, 3)); // convert to rounded (to the nearest thousandth) string
   $thous_pos = strlen($money_str)-1;                    // Thousandth string position
   $hundt_pos = strlen($money_str)-2;                    // Hundredth string position
   if ($money_str[$thous_pos] === "5") $money_str[$thous_pos] = ((int)$money_str[$hundt_pos] & 1) ? "9" : "0"; // use a bitwise test, its faster than modulus
   // The above statement assists round() by setting the thousandth position to 9 or zero if the hundredth is even or odd (respectively)
   return round($money_str, 2); // Return rounded value
}


for ($i = 0; $i < 10000; $i++) {
  $rand = rand() / 10000;
  $round += round($rand, 2);
  $banker += bankers_round($rand);
  $total += $rand;
}

echo "Total: $total<br>\n";
echo "round() rounding: $round<br>\n";
echo "Banker Rounding: $banker<br>\n";


Results for one run:
Total: 1082648588.0678
round() rounding: 1082648588.52
Banker Rounding: 1082648588.21

Banker is closer in this case.  Total should round to around ..88.07

Next example...

Total: 1087871577.5462
round() rounding: 1087871578.04
Banker Rounding: 1087871577.83

Banker again closer..   should be around  ..77.55


Let's do 100,000 iterations now:

Total: 10769106454.867
round() rounding: 10769106456.21
Banker Rounding: 10769106456.15

Hmm.    ...54.xx is a bit different than ..56.xx.   Technically the banker one is still closer, but you can see how after a while the numbers start to drift apart if you don't (can't?) maintain the precision to keep exact values.

I definitely don't want to be an accountant when I grow up.

-TG








= = = Original message = = =

hah yeah, always worth a little skepticism, but it seemed to make some kind of sense.   If you always round up or always round down, that's obviously not right and you end up losing potentially a lot of money or over-estimating the money involved.

Founding up for 5 through 9 and down for 0 through 4 seems like it makes some kind of sense, but apparently it doesn't work out that way.

I'm sure someone out there knows what I'm talking about (it might be the first time, but I know I'm not making this up hah), but rounding 0.75 up to 0.8 and 0.65 down to 0.6 (or vice versa) is supposed to be more accurate or at least leads to fewer anomalies.

Someone feel like writing a quick script that generates random numbers and does the rounding based on these two ideas (doing it the 'hard way') and see how much variation there is after like 10,000 iterations?  If I have time later, I'll do it.  Now I'm even more curious.

-TG

= = = Original message = = =

<snip>
>  Supposedly this is an accounting trick that 
> ultimatley works out in the end for proper rounding of money 
> values.  

Yeah works out for who? Bet it doesn't for the guy paying :P



___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux