加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP数学转换金额

发布时间:2020-12-13 16:52:39 所属栏目:PHP教程 来源:网络整理
导读:我试图转换板球,即6表示0.6和12表示1.6.我得到了所有工作,除了它返回完整数字的最后一部分. 我的代码: foreach($numberofballs as $x){ $first = floor($x / 6); $last = $x - ($first * 6); echo $first.'.'.$last;} 让我们分配一个数组进行测试,假设需要
我试图转换板球,即6表示0.6和12表示1.6.我得到了所有工作,除了它返回完整数字的最后一部分.

我的代码:

foreach($numberofballs as $x){
    $first = floor($x / 6);
    $last = $x - ($first * 6);
    echo $first.'.'.$last;
}

让我们分配一个数组进行测试,假设需要为这个循环转换下面的数组

$numberofballs = array(1,2,3,4,5,6);

foreach($numberofballs as $x){
    $first = floor($x / 6);
    $last = $x - ($first * 6);
    echo $first.'.'.$last;
}

/* notes
for 1 it does it right = 0.1
for 2 it does it right = 0.2
for 3 it does it right = 0.3
for 4 it does it right = 0.4
for 5 it does it right = 0.5
how its supposed to work for 6:
for 6 I do not want to get = 1 I would like to get 0.6 and no there is never 0.7
/ end notes */

我不是说上面的代码是错误的,我只是想让最终值正确.

解决方法

尝试这样的事情:

foreach( $numberofballs as $x){


       $first = floor($x / 6);
       $last = $x - ($first * 6);
       if($last==0 && $first>0) {$last=6; $first-=1;}
       echo $first.'.'.$last;
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读