Lennart Ericson
asked on
sum doesn't work
With this formula (formula 1)
$summa = ($a_100*$p_100_euro+$a_200 *$p_200_eu ro+$a_700* $p_700_eur o+$a_701*$ p_701_euro +$a_702*$p _702_euro+ $a_703*$p_ 703_euro+$ a_704*$p_7 04_euro+$a _705*$p_70 5_euro+$a_ 706*$p_706 _euro+$a_7 07*$p_707_ euro+$a_70 8*$p_708_e uro+$a_709 *$p_709_eu ro+$a_800* $p_800_eur o+$a_801*$ p_801_euro +$a_802*$p _802_euro+ $a_803*$p_ 803_euro+$ a_804*$p_8 04_euro+$a _805*$p_80 5_euro+$a_ 806*$p_806 _euro+$a_8 07*$p_807_ euro+$a_80 8*$p_808_e uro+$a_809 *$p_809_eu ro+$a_810* $p_810_eur o+$a_811*$ p_811_euro +$a_812*$p _812_euro+ $a_813*$p_ 813_euro+$ a_816*$p_8 16_euro+$a _817*$p_81 7_euro+$a_ 822*$p_822 _euro+$a_8 30*$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_eu ro+$a_700* $p_700_eur o+$a_701*$ p_701_euro +$a_702*$p _702_euro+ $a_703*$p_ 703_euro+$ a_704*$p_7 04_euro+$a _705*$p_70 5_euro+$a_ 706*$p_706 _euro+$a_7 07*$p_707_ euro+$a_70 8*$p_708_e uro+$a_709 *$p_709_eu ro+$a_800* $p_800_eur o+$a_801*$ p_801_euro +$a_802*$p _802_euro+ $a_803*$p_ 803_euro+$ a_804*$p_8 04_euro+$a _805*$p_80 5_euro+$a_ 806*$p_806 _euro+$a_8 07*$p_807_ euro+$a_80 8*$p_808_e uro+$a_809 *$p_809_eu ro+$a_810* $p_810_eur o+$a_811*$ p_811_euro +$a_812*$p _812_euro+ $a_813*$p_ 813_euro+$ a_816*$p_8 16_euro+$a _817*$p_81 7_euro+$a_ 822*$p_822 _euro+$a_8 30*$p_830_ euro";
it prints out like: 1*1 905+0*1 461+0*100+1*388+0*265+0*10 9+0*31+0*6 04+0*137+0 *238+0*1 581+0*1 503+0*173+0*311+0*265+1*38 1+0*355+0* 84+0*133+0 *249+0*87+ 0*425+0*48 5+0*27+0*2 58+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.
$summa = ($a_100*$p_100_euro+$a_200
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
it prints out like: 1*1 905+0*1 461+0*100+1*388+0*265+0*10
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.
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@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.
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.
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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!
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!