如何获得整数的小数点

时间:2019-04-23 09:57:15

标签: php regex

我想获取费用明细,但我在小数点上存货,它应该是我下面的fill.out输出。请查看并检查我的代码(如果缺少)。确实可以帮到我很多。错误显示我的代码输出。

$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."\n";
        }
        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."\n";
            }else{
                echo $remainder."\n";
            }
            $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."\n";
        }
    } else {
        if($difference < 0){
            echo $remainder."\n";
        } else {
            $count = 0;
            for ($i=1; $i <= ceil($installment); $i++) { 
                $count = $count + 1;
                if($counter != ceil($installment)){
                    echo $base."\n";                        
                }else{
                    echo $remainder."\n";
                }                   
                $counter = $counter + 1;
            }
        }
    }
}

这是我的代码的输出。

750
750
100
0.50

但是正确的输出是这个。

750
750
100.50

我希望有人能帮助我解决这个问题。花了好几个星期,但我无法解决这个问题。

3 个答案:

答案 0 :(得分:1)

我真的不明白您要做什么,但是用部分函数编写代码不是更好吗?

无论如何,通过编辑代码的这一部分,我设法输出想要的结果:

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

if (is_float($charge)) {

    // echo "TRUE";

    if($remainder == 0){

        $count = 0;

        for ($i=1; $i <= round($installment); $i++) {
            $count = $count + 1;

            echo $base."\n";


        }

        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."\n";
            }

            $counter = $counter + 1;

        }

        if(is_float($charge)){

// if you want to concatinate the number to previuse one you must do it here
// the extra 0.5 is echo here

            $exploadedAmount = explode('.', $charge);

            echo "$remainder.".$exploadedAmount[1];

        }
    }
}
else{

    if($remainder == 0){

        $count = 0;

        for ($i=1; $i <= ceil($installment); $i++) {
            $count = $count + 1;

            echo $base."\n";
        }

    }
    else{

        if($difference < 0){

            echo $remainder."\n";

        }else{

            $count = 0;
            for ($i=1; $i <= ceil($installment); $i++) {

                $count = $count + 1;

                if($counter != ceil($installment)){

                    echo $base."\n";

                }else{

                    echo $remainder."\n";

                }

                $counter = $counter + 1;

            }
        }
    }
}

输出将是: 750 750 100.5

答案 1 :(得分:1)

我不知道我是否正确面对了您的问题,但是根据您期望的输出,以下将解决问题。

else{

    $count = 0;
    for ($i=1; $i <= ceil($installment); $i++) { 

        $count = $count + 1;

        if($counter != ceil($installment)){
            echo $base."\n";
        }else{

            // echo $remainder."\n"; 
            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 :(得分:1)

  

回显“。”。$ exploadedAmount [1]。“ 0”;

在第63行,如果u更改该数字并在开头和结尾加0,则意味着您可以获得准确的输出,并且也适用于其他电话号码。