Problems with float after require

Topics: Developer Forum
Apr 28, 2010 at 7:27 AM

Hi there,

I want to use PHPExcel for a PDF and Excel2007 dump of our forms in PHP. We detected that PHP doesnt round at least 1 specific float.
Here is the Code to test:

<?php
echo "<pre>\n";
$float1 = 580.9299999999999;
$string1 = "580.9299999999999";
var_dump($float1);
echo "<br>\n";
var_dump($string1);
echo "<br>\n";

require_once 'library/PHPExcel/IOFactory.php';

var_dump($float1);
echo "<br>\n";
var_dump(round($float1,2));
echo "<br>\n";
var_dump($string1);
echo "<br>\n";

die();
?>

What´s going wrong?

Coordinator
Apr 28, 2010 at 7:46 AM
Edited Apr 28, 2010 at 7:47 AM

I don't know what's going on, but what makes you think it has anything to do with PHPExcel?

If I run your script, I get:

float(580.93)

string(17) "580.9299999999999"

float(580.93)

float(580.93)

string(17) "580.9299999999999"
If I comment out the require_once line, I get
float(580.93)

string(17) "580.9299999999999"

float(580.93)

float(580.93)

string(17) "580.9299999999999"

 

What exactly are you expecting to see?

If this is simply a generic issue with computer handling of floating point numbers, take a look at this article and be aware that it affects almost every computer language on almost every computer platform

Apr 28, 2010 at 8:04 AM
Edited Apr 28, 2010 at 8:06 AM
I´m seeing: float(580.93) string(17) "580.9299999999999" float(580.9299999999999) float(580.9299999999999) string(17) "580.9299999999999" I´m using PHP Version: C:\Users\Wehner> php -v PHP 5.2.11 (cli) (built: Nov 8 2009 15:10:54) Copyright (c) 1997-2009 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies with Zend Extension Manager v5.1, Copyright (c) 2003-2009, by Zend Technolog ies - with Zend Data Cache v4.0, Copyright (c) 2004-2009, by Zend Technologies [ loaded] [licensed] [disabled] - with Zend Utils v1.0, Copyright (c) 2004-2009, by Zend Technologies [loade d] [licensed] [enabled] - with Zend Optimizer+ v4.1, Copyright (c) 1999-2009, by Zend Technologies [ loaded] [licensed] [disabled] - with Zend Debugger v5.2, Copyright (c) 1999-2009, by Zend Technologies [lo aded] [licensed] [enabled]
Coordinator
Apr 28, 2010 at 8:11 AM

If it really is a concern (and note that this only changes display rathe rthan the internal storage of a number in PHP), edit the Classes/PHPExcel/Calculation/Function.php file, and at around lines 60-63 you'll find a block of code like:

$savedPrecision = ini_get('precision');
if ($savedPrecision < 16) {
 ini_set('precision',16);
}

Simply comment this out.

Apr 28, 2010 at 8:14 AM

Thanks, it´s working