PHPExcel unexpected float behavior

前端 未结 1 565
一生所求
一生所求 2021-01-19 04:11

I\'m create a new PHPExcel object into one of my projects and whats odd is how some float functions behave after the library is included. Take for example the round

1条回答
  •  隐瞒了意图╮
    2021-01-19 05:01

    This comes down to PHPExcel modifying the php.ini precision setting in its calculation engine. You can override this yourself:

    echo round(90.00 + 9.71, 2); // outputs 99.71
    
    $savedPrecision = ini_get('precision');
    $sheet = new \PHPExcel();
    ini_set('precision', $savedPrecision);
    
    echo round(90.00 + 9.71, 2); // outputs 99.71
    

    or alternatively, use sprintf() or number_format() when you echo float values to get the number of decimals that you want to display

    0 讨论(0)
提交回复
热议问题