views:

569

answers:

5

I'm trying to calculate the sum of an array of decimal values in PHP, but for some reason it keeps rounding to integers.

for example:

$oldArray = array(0.00,1000.11,988.92,978.22,964.01,953.07,948.82,917.26,902.56,913.21,904.08,898.86,892.79);
$myVar = 0.0;
for($k=1;$k<10;$k++)
{
$myVar += $oldArray[$k];
}
print_r($myVar);

$oldArray is actually populated with decimal values from an SQL query (the length of $oldarray is about several hundred, but I want the first 10. In the above example, I'm expecting $myVar to be a decimal, but it turns out to be just an integer. I tried setting $myVar = 0.0000 before the for loop, I tried $myVar += $oldArray[$k] + 0.0000, etc but nothing seems to work.

What am I doing wrong? How do I explicitly set $myVar to be a decimal?

+3  A: 

Can't reproduce this.

php > $oldArray = array(0, .1, .2, .3, .4, .5, .6, .7, .8, .9);                                                    
php > $myVar = 0.0;
php > for($k=0;$k < count($oldArray);$k++)
php > {
php { $myVar += $oldArray[$k];
php { }
php > print_r($myVar);
4.5

EDIT: I tried the code in your comment, and it's fine. Like AlbertoPL, I suspect the problem is elsewhere.

php > $oldArray = array(0.01,1000.11,988.92,978.22,964.01,953.07,948.82,917.26,902.56,913.21,904.08,898.86,892.79);
php > $myVar = 0.0000;
php > for($k=1;$k<10;$k++)
php >   $myVar += $oldArray[$k];
php > print_r($myVar);
8566.18
Matthew Flaschen
$oldArray = array(0.01,1000.11,988.92,978.22,964.01,953.07,948.82,917.26,902.56,913.21,904.08,898.86,892.79); $myVar = 0.0000;for($k=1;$k<10;$k++)$myVar += $oldArray[$k];print_r($myVar);i get a whole number -.-
Also ran in PHP console, got 8566.18 =/
Matchu
+4  A: 

You can try using array_sum() instead and use (float) to cast the values. Additionally I would make sure that the values in the array are in the correct format (1.45 and not 1,45). HTH.

Update

Btw. you can use "is_float()" to check every parameter in the array.

merkuro
I can't use array_sum() because I only want the sum for a portion of the array. And I tried it anyway and it still gave the whole number.
+1 for the hint on number format
Eineki
you can use arry_sum(array_slice($oldArray,1,10)) to have the sum of the first 10 element of oldArray (excluding the 0th element)
Eineki
+10  A: 

Given that this seems impossible to reproduce, to me it sounds like a problem with your PHP environment itself.

Check php.ini for a setting called "precision", and make sure it's set to the default of 14 significant figures. I can't imagine why this would be changed, but it would definitely have an impact.

Matchu
yeah, i tried ini_set("precision", 14); and now it works great. thanks!
+1 Impressive :)
merkuro
+1 great suggestion
Eineki
Cool :) You'll definitely wanna figure out the root of that problem, too, so you don't have to work with it ever again >_<
Matchu
A: 

Use array_chunk() and array_sum()

A: 

I know that this has already been answered, but if you are trying to sum values from MySQL, I would suggest this as an often fastest alternative:

SELECT SUM( `column_name` ) FROM `table_name` LIMIT 10;
Christopher W. Allen-Poole