Php subtract returns a scientific notation value

53 views Asked by At

I'm doing calculations for the accounting. When I trying the subtraction, it returns a scientific notation value.

Below is Sample Data for the Calculation

Year is 2021 
   [debit is 0.00]
   [credit is 0.00] 
   [Total is 0] 
   [OB is 0]
Year is 2022 
   [debit is 0.00] 
   [credit is 1229.40] 
   [Total is -1229.4] 
   [OB is -1229.4]
Year is 2023 
   [debit is 55048.52] 
   [credit is 53819.12] 
   [Total is 1229.4] 
   [OB is -5.911715561524E-12] 

Below is code for calculation

function ob($code,$year){
    $begin_year = report_begin_year();
    $bal        = 0;
    $m          = 'all';
    
    if($year == $begin_year) {
        return 0;
    }else{
        $year       = (is_array($year) && count($year) > 0 ? $year : explode(" ",$year));
        if(is_array($year)){
            $d  = str_replace( ',', '', get_total_debit($code,$m,$year[0]));
            $c  = str_replace( ',', '', get_total_credit($code,$m,$year[0]));  
            $t  = $d - $c;
            $ob = 0;
            for($i=$begin_year;$i<$year[0];$i++){                             
                $db = str_replace( ',', '', get_total_debit($code,$m,$i));
                $cr = str_replace( ',', '', get_total_credit($code,$m,$i)); 
                $tot= $db - $cr;
                $ob = $ob + $tot;   
            }            
            $bal = $ob;
            return $bal;
        }
    }
}

Function calling ob(23,2024); It returns the value -5.911715561524E-12 But it should return 0;

Anyone can please help me to fix this. Thanks in Advance

1

There are 1 answers

0
Karl Hill On

Use the round() function to round the result to the nearest integer.

$bal = round($ob, 0); // Round the result to the nearest whole number

return $bal;