2

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

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.

Toto
  • 89,455
  • 62
  • 89
  • 125
rjjvillareal
  • 49
  • 1
  • 7
  • 1
    What exactly are you trying to achieve? Output the payments necessary to pay off `$charge` with a maximum payment of `$base`? – Nick Apr 23 '19 at 10:18
  • What have you tried to debug the problem? That's a lot of code, and for you (having knowledge about that algorithm) it should not be too hard to debug it – Nico Haase Apr 23 '19 at 12:51

3 Answers3

1

I really don't understand what are you trying to do but isn't it better to write the code in partly functions?

anyways by editing this part of your code I manage to output the result you want:

$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;

            }
        }
    }
}

The output will be : 750 750 100.5

Salar Bahador
  • 1,433
  • 1
  • 14
  • 26
1

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

    }
}
rené
  • 114
  • 6
  • how about will change the value into 1500.00? its goes some error. it getting offset online number – rjjvillareal Apr 24 '19 at 07:21
  • the error is caused by your string splitting in the other sections. $exploadedAmount = explode('.', $charge); $exploadedAmount[1]; PHP will render the Value 1500.0 => 1500. Your splitting with the explode function returns only an array with Index 0. So you cant access index 1. – rené Apr 24 '19 at 11:40
  • You shoud stay away from string operations. Use math operations to do math ;-) – rené Apr 24 '19 at 11:43
1

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

At line number 63 , if u change that number and 0 adding at the beginning and end means your can get that accurate output and also it is applicable for other phone numbers also.

ChaN
  • 11
  • 2