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条)

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致