Php: when a number gets to 0.6 make it 1

本秂侑毒 提交于 2019-12-12 06:48:52

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!