sum doesn't work

Lennart Ericson
Lennart Ericson used Ask the Experts™
on
With this formula (formula 1)

$summa = ($a_100*$p_100_euro+$a_200*$p_200_euro+$a_700*$p_700_euro+$a_701*$p_701_euro+$a_702*$p_702_euro+$a_703*$p_703_euro+$a_704*$p_704_euro+$a_705*$p_705_euro+$a_706*$p_706_euro+$a_707*$p_707_euro+$a_708*$p_708_euro+$a_709*$p_709_euro+$a_800*$p_800_euro+$a_801*$p_801_euro+$a_802*$p_802_euro+$a_803*$p_803_euro+$a_804*$p_804_euro+$a_805*$p_805_euro+$a_806*$p_806_euro+$a_807*$p_807_euro+$a_808*$p_808_euro+$a_809*$p_809_euro+$a_810*$p_810_euro+$a_811*$p_811_euro+$a_812*$p_812_euro+$a_813*$p_813_euro+$a_816*$p_816_euro+$a_817*$p_817_euro+$a_822*$p_822_euro+$a_830*$p_830_euro);

I get a wrong print out. It prints out $summa = 770.

Changing the formula to (formula 2):

$summa = "$a_100*$p_100_euro+$a_200*$p_200_euro+$a_700*$p_700_euro+$a_701*$p_701_euro+$a_702*$p_702_euro+$a_703*$p_703_euro+$a_704*$p_704_euro+$a_705*$p_705_euro+$a_706*$p_706_euro+$a_707*$p_707_euro+$a_708*$p_708_euro+$a_709*$p_709_euro+$a_800*$p_800_euro+$a_801*$p_801_euro+$a_802*$p_802_euro+$a_803*$p_803_euro+$a_804*$p_804_euro+$a_805*$p_805_euro+$a_806*$p_806_euro+$a_807*$p_807_euro+$a_808*$p_808_euro+$a_809*$p_809_euro+$a_810*$p_810_euro+$a_811*$p_811_euro+$a_812*$p_812_euro+$a_813*$p_813_euro+$a_816*$p_816_euro+$a_817*$p_817_euro+$a_822*$p_822_euro+$a_830*$p_830_euro";

it prints out like: 1*1 905+0*1 461+0*100+1*388+0*265+0*109+0*31+0*604+0*137+0*238+0*1 581+0*1 503+0*173+0*311+0*265+1*381+0*355+0*84+0*133+0*249+0*87+0*425+0*485+0*27+0*258+0*391+0*26+0*123+0*53+0*426

If I add it manually together I get 2674. And that is what I expected formula 1 would have given me.

What have I done to cause this miscalculation? It might be a typo, but I can't find it.

Please, gurus, advice me.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Is it just an artifact of how you posted, or do you actually have spaces in the values?  e.g. 1*1 905+0*1
I guess what I'm asking, more specifically is the value of $p_100_euro equal to

1905

or is it equal to

1 905

?
@hmarcbower - Considering that the manual value 2674 subtract the number you're asking about 1905, is 769, that's awfully close to 770, which the server is calculating it as, so my guess is yeah -- that value is a string, and not a number.
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

So long story short lericson, you've got a value ($p_100_euro) that's a string, (which essentially has the value of 1 in this scenario)... so you need to either convert it to an integer in your code here, or correct it at the source.
That was just one of the variables - there appear to be several that have a space in them when over 999.  Hopefully it's just a type thing. :)

Try this:  Just multiply your first pair and see what you get:

$a_100*$p_100_euro

Do the 200s as well, as that one has a space in it too.
If you'd like to convert it in your code, you can replace $p_100_euro with the following intval(str_replace(" ","",$p_100_euro))

Author

Commented:
jordanrynard and hmarcbower,
Had $p_100_euro = number_format($p_100_eu, 0, ',', ' ');
Changed it to $p_100_euro = number_format($p_100_eu, 0, ',', '');
You guys were spot on. Thanks!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial