dtrz17917 2019-04-23 09:57
浏览 102
已采纳

如何获得整数的小数点

I want to get the breakdown of the charges but im stock on the decimal point, it should be the output i fill.out below. please see and check my code if there is lacking. it really could help me much. Ill show the output of my code.

$charge = 1600.50;
$base = 750;
$difference = $charge - $base;
$installment = $charge / 750;
$remainder = ($charge % 750);
$counter = 1;

if (is_float($charge)) {
    if($remainder == 0) {
        $count = 0;
        for ($i=1; $i <= round($installment); $i++) {
            $count = $count + 1;
            echo $base."
";
        }
        if(is_float($charge)){
            $exploadedAmount = explode('.', $charge);
            echo "0.".$exploadedAmount[1];              
        }
    } else {
        $count = 0;
        for ($i=1; $i <= ceil($installment); $i++) { 
            $count = $count + 1;
            if($counter != ceil($installment)){
                echo $base."
";
            }else{
                echo $remainder."
";
            }
            $counter = $counter + 1;
        }
        if(is_float($charge)){
            $exploadedAmount = explode('.', $charge);
            echo "0.".$exploadedAmount[1];              
        }
    }
} else {
    if($remainder == 0){
        $count = 0;
        for ($i=1; $i <= ceil($installment); $i++) { 
            $count = $count + 1;
            echo $base."
";
        }
    } else {
        if($difference < 0){
            echo $remainder."
";
        } else {
            $count = 0;
            for ($i=1; $i <= ceil($installment); $i++) { 
                $count = $count + 1;
                if($counter != ceil($installment)){
                    echo $base."
";                        
                }else{
                    echo $remainder."
";
                }                   
                $counter = $counter + 1;
            }
        }
    }
}

This is the output of my code.

750
750
100
0.50

But the correct output would be this.

750
750
100.50

I hope there is anyone could help me to solve this problem. it took weeks but im not able to solve this.

  • 写回答

3条回答 默认 最新

  • douchan0523 2019-04-23 10:31
    关注

    I don't know if i faced your problem correctly, but according to your desired output the following will do the trick.

    else{
    
        $count = 0;
        for ($i=1; $i <= ceil($installment); $i++) { 
    
            $count = $count + 1;
    
            if($counter != ceil($installment)){
                echo $base."
    ";
            }else{
    
                // echo $remainder."
    "; 
                echo $remainder + $charge-floor($charge); // get fractial part of your $charge and add it to your remainder
            }
            $counter = $counter + 1;
    
        }
    
        /* unnecessary if-statement, is already checked by surrounding if-statement
        if(is_float($charge)){  
        */
    
        // echo 'TRUE';
        // $exploadedAmount = explode('.', $charge);
        // echo "0.".$exploadedAmount[1];              
    
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c