Avatar of Lennart Ericson
Lennart EricsonFlag for Sweden

asked on 

sum doesn't work

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.
PHP

Avatar of undefined
Last Comment
Lennart Ericson
Avatar of hmarcbower
hmarcbower
Flag of Canada image

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
Avatar of hmarcbower
hmarcbower
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of jordanrynard
jordanrynard

@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.
Avatar of jordanrynard
jordanrynard

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.
Avatar of hmarcbower
hmarcbower
Flag of Canada image

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.
SOLUTION
Avatar of jordanrynard
jordanrynard

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Lennart Ericson

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!
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo