问题描述
我正在我的一个项目中创建一个新的PHPExcel对象,奇怪的是,在包含库之后,某些float函数的行为方式.以round
函数为例,在包含PHPExcel后它不会四舍五入.
echo round(90.00 + 9.71, 2); // outputs 99.71
$sheet = new \PHPExcel();
$sheet->getProperties()
->setCreator('Bob')
->setTitle('Title');
echo round(90.00 + 9.71, 2); // outputs 99.7099999999
exit;
有没有解决的办法?我尝试使用setValueExplict
,但提示未找到PHPExcel_Cell_DataType::TYPE_STRING
.
这归因于PHPExcel修改了其计算引擎中的php.ini precision
设置.您可以自己覆盖此内容:
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
或者,当您回显浮点值以获取要显示的小数位数时,请使用sprintf()或number_format()
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
function, it will not round after PHPExcel is included.
echo round(90.00 + 9.71, 2); // outputs 99.71
$sheet = new \PHPExcel();
$sheet->getProperties()
->setCreator('Bob')
->setTitle('Title');
echo round(90.00 + 9.71, 2); // outputs 99.7099999999
exit;
Is there a way around this? I tried using setValueExplict
but it says PHPExcel_Cell_DataType::TYPE_STRING
is not found.
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
这篇关于PHPExcel意外的浮动行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!