Php: when a number gets to 0.6 make it 1

2019-09-19 19:20发布

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'] ?>

2条回答
萌系小妹纸
2楼-- · 2019-09-19 19:43

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.

查看更多
beautiful°
3楼-- · 2019-09-19 19:54
$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

查看更多
登录 后发表回答