Php 减法返回科学记数法值

问题描述 投票:0回答:1

我正在做会计计算。当我尝试减法时,它返回一个科学计数值。

以下是计算示例数据

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] 

以下是计算代码

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

函数调用 ob(23,2024); 它返回值-5.911715561524E-12 但它应该返回0;

任何人都可以帮我解决这个问题。预先感谢

php scientific-notation
1个回答
0
投票

使用

round()
函数将结果舍入为最接近的整数。

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

return $bal;
© www.soinside.com 2019 - 2024. All rights reserved.