-2

I am trying to calculate how much the commission is off of an array I am pulling from an API. The following is my code.

$mst_per_off_base=   $pri['base']  * '.01'; // calculate 1% of the base price
$gr[] = $pri['com'] - $mst_per_off_base ; // deduct the 1% off of the com
echo array_sum($gr); // total of all after deduction

$pri['base'] and $pri['com'] represent more than one number. The problem is that it is showing the math.

For example, if $gr equals 50.85 then it is echoing 50.85101.7152.55 I would like it just to just show 152.55.

Any suggestion?

A Kaufman
  • 55
  • 5
  • 3
    You can't just do math *at* an array like that, and beside that you absolutely **should not** use floating point numbers to represent currency unless you're ok with straight up losing money. 1. Use something like [moneyphp](https://packagist.org/packages/moneyphp/money). 2. Use a loop to do the math against each *item* in the arrays, not the arrays themselves. – Sammitch Feb 13 '18 at 20:05
  • What values do you have in those anonymous variables? – Andreas Feb 13 '18 at 20:05
  • Because you echo on each iteration? – u_mulder Feb 13 '18 at 20:07
  • 1
    If that's real code you should be getting: _Notice: Use of undefined constant mst_per_off_base - assumed 'mst_per_off_base'_ – AbraCadaver Feb 13 '18 at 20:12
  • Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) –  Feb 13 '18 at 20:16
  • @rtfm It is broken but I suspect this issue has more to do with using strings in equations but it doesn't feel worthwhile to test and verify given the limited amount of info provided in the question. – MonkeyZeus Feb 13 '18 at 20:23
  • @MonkeyZeus probably, i gave the question only a quick look –  Feb 13 '18 at 20:25
  • 1
    Is this code in a loop? I think you're just seeing the `echo` that it does each time through the loop. – Barmar Feb 13 '18 at 20:26
  • 1
    There's simply no way that `echo array_sum(...)` will echo more than one number. – Barmar Feb 13 '18 at 20:27
  • The OP code is incomplete -- needs to show where $pri defined and takes on a value. – slevy1 Feb 13 '18 at 20:27

3 Answers3

2

It's bad enough to try and natively perform math calculations with floats, it's double-ly (pun, ha, get it?) bad to interleave strings into your equations.

You need PHP's bcmath.

Look into bcmul() and bcadd() in particular.

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
1

Here's an illustration of what needs to happen:

<?php

// suppose:
$pri['base'] = 30000;
$pri['com'] = 5000;

// then
$mst_per_off_base =   bcmul($pri['base'],.01); // calculate 1% of the base price
$gr[] = bcsub($pri['com'],$mst_per_off_base); // deduct the 1% off of the com
echo array_sum($gr); // total of all after deduction

I had to correct the OP code. "mst_per_off_base" needed to be prepended with a dollar sign. And, this was a case of subtraction, so use bcsub() and not bcadd (unless you wish to add a negative number with a positive one). Lastly, it's extremely important to include as complete an example as is necessary for the code to run correctly. So, I defined $pri myself to pose a theoretical example.

See live code

slevy1
  • 3,797
  • 2
  • 27
  • 33
0

@bamar is correct I accidentally nested it in a loop.

Thank you for all of your suggestions regarding the proper way to do the math I have some reading to do...

A Kaufman
  • 55
  • 5