问题
i am making a cricket stats page and in 1 over there are 6 balls, however if i put in 0.7 id dose not make it 1.1, how can i use php to make it do this?
This is all the code i have got (im using mysql):
<?php echo $row['o'] ?>
回答1:
$x = 0.7;
$overs = floor(($x * 10) / 6);
$balls = ($x * 10) - ($overs * 6);
echo $overs.'.'.$balls;
But you might want to use an integer input (the number of balls bowled) rather than a decimal value.
$x = 7;
$overs = floor($x / 6);
$balls = $x - ($overs * 6);
echo $overs.'.'.$balls;
This logic can be simplified using the % modulus operator, but I've shown it longhand to try and help you understand the principle
回答2:
Disclaimer: Please provide code to help people know what you have done, what is not working and what you want. But since I love Cricket, you need to do something like below. Giving C# code since you did not provide your initial PHP code.
Use equivalent PHP code:
int balls = 14;
string overs = balls/6 + "." + balls%6;
will give you "2.2" Also 5 balls comes out as 0.5 which is what you want as well.
来源:https://stackoverflow.com/questions/5754806/php-when-a-number-gets-to-0-6-make-it-1