I'm trying to write some code to add together cricket overs. Over numbering in cricket goes 0.1, 0.2, 0.3, 0.4, 0.5, 1.0, 1.1 etc and so just adding together decimals in a standard way doesn't work.
I've followed the advice provided in this thread - Php: when a number gets to 0.6 make it 1 to do the actual conversion, and when I throw a random number into the equation provided it returns as expected. However, when I use a number that I am generating from my database, it doesn't return the same thing.
I have tried to create some example code and output to explain the problem further. In this example the variable $xb is equal to 1.2 as you will see in the output.
Code:
echo "Var type - " . gettype($xb) . ' Value = ' . $xb . '<br />';
echo "Var type - " . gettype(1.2) . ' Value = 1.2<br />';
$overs = floor((1.2 * 10) / 6);
$balls = (1.2 * 10) - ($overs * 6);
$x_overs = floor(($xb * 10) / 6);
$x_balls = ($xb * 10) - ($x_overs * 6);
echo "Total overs = " . $overs . '.' . $balls . "<br />";
echo "Total x overs = " . $x_overs . '.' . $x_balls . "<br />";
Output:
Var type - double Value = 1.2
Var type - double Value = 1.2
Total overs = 2.0
Total x overs = 1.6
I'm sure I must be doing something fundamentally stupid or be misunderstanding something basic, but I've been staring at it for an hour and can't see it. Can someone put me out my misery please?