Recipes‎ > ‎Programming‎ > ‎

PHP

Bankers' Round

This Bankers' Rounding function takes uses pseudo-int math to eliminate issues related to float math, and rounds to the specified precision.  Limited by the size of your (int).

function bround( $value, $precision )
{
    // http://sites.google.com/site/cjastram
    $factor = pow( 10, $precision );
    $a = $value;
    $a = $a * $factor;
    $dec = (int) floor( $a );
    $frac = $a - $dec;
    $extra = (int) floor( $frac * 10 );
    if( $extra > 5 )
    {
        $dec++;
    }
    elseif( $extra == 5 )
    {
        if( $dec & 1 )
        {
            // odd, round up
            $dec++;
        }
    }
    return $dec / $factor;
}

PHPUnit under Windows 7

This assumes your PHP install dir is C:\PHP.  If you accepted the defaults when installing, it is probably under C:\Program Files\PHP or C:\Program Files (x86)\PHP..
  1. Install PHP 5.3.3 with the regular installer
  2. CMD: In c:\php, run go-pear, accepting defaults, run created .REG file afterwards
  3. pear channel-discover pear.phpunit.de
  4. pear install --alldeps phpunit/PHPUnit
    1. You'll need to edit the actual phpunit script in your PHP install directory, to change .\php.exe to the actual PHP directory.
  5. pear install Testing_Selenium-0.4.3


Comments