如何修复 PhpExcel 额外的小数点错误?
How to fix PhpExcel extra decimal points bug?
我正在将 PHPExcel 用于我正在处理的客户项目。我一直面临这个错误,在读取 excel 文件时,会为某些单元格随机添加额外的小数位。例如,我得到的不是 56.25
,而是 56.24999999999
。我已经将这个问题追溯到 PHPExcel 本身,我花了很多时间查看文档,但我还没有找到任何可以解决这个问题的方法。我不能只将数字四舍五入到给定的小数点,因为它是随机发生的,不同的单元格有不同的小数点。请帮忙!!!
编辑:小数点必须与手动输入的完全一致,这一点至关重要。因此,如果客户在一个单元格中手动输入 34.25,然后在另一个单元格中手动输入 51.3456,它们应该保持原样!因此,四舍五入不是理想的解决方案。
查看 http://www.php.net/manual/en/function.round.php
echo round(56.24999999999, 2);
输出将是 56.25
编辑尝试使用这个自定义函数http://www.php.net/manual/en/function.round.php#102641
<?php
function round_up ( $value, $precision ) {
$pow = pow ( 10, $precision );
return ( ceil ( $pow * $value ) + ceil ( $pow * $value - ceil ( $pow * $value ) ) ) / $pow;
}
echo round_up(56.24999999999, 2); // 56.25
?>
阅读 Mark Baker 的评论后,
These aren't random; decimal float values cannot always be represented exactly on
computers that use binary representation.... this is not a PHPExcel problem, or even a PHP problem.... it's a well-documented digital computer problem; and the solutions for formatting are provided by sprint() or number_format() –
我发现确保所有数字都保存为文本可以解决这个问题。 PHP 会将它们读取为字符串,因此其奇怪的浮点恶作剧不会成为问题。
我也注意到了这一点。对我有用的最快解决方案是对 PHPExcel/Calculation.php
文件进行小的修改:
前往 PHPExcel/Calculation.php
将 $setPrecision = (PHP_INT_SIZE == 4) ? 14 : 16;
更改为较低的值,如
$setPrecision = (PHP_INT_SIZE == 4) ? 4 : 4;
如果您有 OS x86 PHP_INT_SIZE==4 为真,否则您有 OS x64。
16 对于 OS x64 来说太长了。
就我而言,我有一个 OS x64,所以我更改 PHPExcel/Calculation.php
$setPrecision = (PHP_INT_SIZE == 4) ? 14 : 16;
到
$setPrecision = (PHP_INT_SIZE == 4) ? 14 : 14;
工作!!!
我正在将 PHPExcel 用于我正在处理的客户项目。我一直面临这个错误,在读取 excel 文件时,会为某些单元格随机添加额外的小数位。例如,我得到的不是 56.25
,而是 56.24999999999
。我已经将这个问题追溯到 PHPExcel 本身,我花了很多时间查看文档,但我还没有找到任何可以解决这个问题的方法。我不能只将数字四舍五入到给定的小数点,因为它是随机发生的,不同的单元格有不同的小数点。请帮忙!!!
编辑:小数点必须与手动输入的完全一致,这一点至关重要。因此,如果客户在一个单元格中手动输入 34.25,然后在另一个单元格中手动输入 51.3456,它们应该保持原样!因此,四舍五入不是理想的解决方案。
查看 http://www.php.net/manual/en/function.round.php
echo round(56.24999999999, 2);
输出将是 56.25
编辑尝试使用这个自定义函数http://www.php.net/manual/en/function.round.php#102641
<?php
function round_up ( $value, $precision ) {
$pow = pow ( 10, $precision );
return ( ceil ( $pow * $value ) + ceil ( $pow * $value - ceil ( $pow * $value ) ) ) / $pow;
}
echo round_up(56.24999999999, 2); // 56.25
?>
阅读 Mark Baker 的评论后,
These aren't random; decimal float values cannot always be represented exactly on computers that use binary representation.... this is not a PHPExcel problem, or even a PHP problem.... it's a well-documented digital computer problem; and the solutions for formatting are provided by sprint() or number_format() –
我发现确保所有数字都保存为文本可以解决这个问题。 PHP 会将它们读取为字符串,因此其奇怪的浮点恶作剧不会成为问题。
我也注意到了这一点。对我有用的最快解决方案是对 PHPExcel/Calculation.php
文件进行小的修改:
前往 PHPExcel/Calculation.php
将 $setPrecision = (PHP_INT_SIZE == 4) ? 14 : 16;
更改为较低的值,如
$setPrecision = (PHP_INT_SIZE == 4) ? 4 : 4;
如果您有 OS x86 PHP_INT_SIZE==4 为真,否则您有 OS x64。 16 对于 OS x64 来说太长了。 就我而言,我有一个 OS x64,所以我更改 PHPExcel/Calculation.php
$setPrecision = (PHP_INT_SIZE == 4) ? 14 : 16;
到
$setPrecision = (PHP_INT_SIZE == 4) ? 14 : 14;
工作!!!