开发者

Php: when a number gets to 0.6 make it 1

i am making a cricket stats page and in 1 over t开发者_开发知识库here 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'] ?>


$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


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.


This is the simple PHP condition for overcoming this problem. The method is Lengthy but working:-

$ball__= 15;    
$over=0;

if($ball__>=6 && $ball__<12){
  $over+=1;
  if($ball__==6){$ball__=0;}
  if($ball__==7){$ball__=1;}
  if($ball__==8){$ball__=2;}
  if($ball__==9){$ball__=3;}
  if($ball__==10){$ball__=4;}
  if($ball__==11){$ball__=5;}
}
if($ball__>=12 && $ball__<18){
  $over+=2;
  if($ball__==12){$ball__=0;}
  if($ball__==13){$ball__=1;}
  if($ball__==14){$ball__=2;}
  if($ball__==15){$ball__=3;}
  if($ball__==16){$ball__=4;}
  if($ball__==17){$ball__=5;}
}
if($ball__>=18 && $ball__<24){
  $over+=3;
  if($ball__==18){$ball__=0;}
  if($ball__==19){$ball__=1;}
  if($ball__==20){$ball__=2;}
  if($ball__==21){$ball__=3;}
  if($ball__==22){$ball__=4;}
  if($ball__==23){$ball__=5;}
}
if($ball__>=24 && $ball__<30){
  $over+=4;
  if($ball__==24){$ball__=0;}
  if($ball__==25){$ball__=1;}
  if($ball__==26){$ball__=2;}
  if($ball__==27){$ball__=3;}
  if($ball__==28){$ball__=4;}
  if($ball__==29){$ball__=5;}
}
if($ball__>=30 && $ball__<36){
  $over+=5;
  if($ball__==30){$ball__=0;}
  if($ball__==31){$ball__=1;}
  if($ball__==32){$ball__=2;}
  if($ball__==33){$ball__=3;}
  if($ball__==34){$ball__=4;}
  if($ball__==35){$ball__=5;}
}
//----and so on up to the number of balls you have to convert into overs
           echo ($over.".".$ball__); to show results
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜