How to convert european(eu) odds to united kingdom(uk) odds ?

March 12, 2013

A bit hard this one , but there is a way to convert EU odds to UK odds in php.

function eu_to_uk_odds($odds){
	if(floor($odds) !== $odds){
		$odds = (string) round($odds - 1,2);
		$oddsArr = explode('.',$odds);
		$f1 = (int) $oddsArr[0].''.$oddsArr[1]; 
		$f2 = str_pad('1', ($f1 >= 10 ? 3 : strlen($f1) ),'0',STR_PAD_RIGHT);
		$minDel = $this->gcd($f1,$f2);
		$odds = $f1 / $minDel .'/'.$f2/$minDel;	
	} else {
		$odds = ($odds-1).'/1';
	}	
	return $odds;	
}

function gcd($a, $b)
{
    if ($a == 0 || $b == 0)
        return abs( max(abs($a), abs($b)) );

    $r = $a % $b;
    return ($r != 0) ?
        $this->gcd($b, $r) :
        abs($b);
}

echo eu_to_uk_odds(2.3);
//13/100
echo eu_to_uk_odds(1.2);
//1/5

Simple as that.

tags:
posted in how to ?, php by Ivan Gospodinow

Follow comments via the RSS Feed | Leave a comment | Trackback URL

Leave Your Comment


Warning: Use of undefined constant XML - assumed 'XML' (this will throw an Error in a future version of PHP) in /home/c2kblate/sites/ivangospodinow.com/wp-content/plugins/wp-syntaxhighlighter/wp-syntaxhighlighter.php on line 1048
 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org