0

I have an HTML form that passes information, specifically a number that I want to add. When I add these values, for some reason a couple of my values are not being added correctly. Here's my original code:

Here is the array being passed from the form:

289.03
439.08
147.35
153.82
163.94
382.76
2359.43
1983.83

And here's the PHP code:

<?php
$netRate = $_POST['netrate'];
$payloads = $_POST['topay'];
$netLoad=0;

for($i=0;$i<=count($payloads);$i++)
    {
        if(!empty($payloads[$i]))
        {
            ///other code is in here/////
            echo '<td width="60" valign="top">'.$netRate[$i].'</td>';   

            $netLoad = $netLoad + $netRate[$i];

            echo "net rate: ". $netRate[$i];
            echo "<br>";
            echo "net load (sum of all loads): ". $netLoad;
            echo "<br>";
        }
    }

?>

These are the results that I get from the echo statements:

net rate: 289.03
net load (sum of all loads): 289.03
net rate: 439.08
net load (sum of all loads): 728.11
net rate: 147.35
net load (sum of all loads): 875.46
net rate: 153.82
net load (sum of all loads): 1029.28
net rate: 163.94
net load (sum of all loads): 1193.22
net rate: 382.76
net load (sum of all loads): 1575.98
net rate: 2,359.43
net load (sum of all loads): 1577.98
net rate: 1,983.83
net load (sum of all loads): 1578.98

So even though the last two $netRate parts of the array are correct (2359.43 & 1983.83) the $netLoad did not add them correctly. What could be the issue here?

1
  • You should use foreach instead of for, there is a significant difference in performance. Commented Jun 16, 2015 at 20:41

2 Answers 2

2

The problem here is that $netRate is actually an arrays of strings. When you use the + operator PHP will convert those strings to numeric values. Unfortunately your system/PHP is recognizing the "," as the decimal character and converts 2,359.43 to 2.35

The usage of floatval would suffer the same problem as the implicit conversion. ie the system would recognize the wrong character as the decimal character.

The seemingly easy way to fix this would be to simply remove any "," from the strings before converting:

// do NOT use this $netLoad = $netLoad + str_replace(",", "", $netRate[$i]);

Unfortunately the usage of "." and "," depends on the regional settings of the user who submitted the form so you can expect different characters depending on who submits the data. (this is why many APIs send values in cents instead of values, eg 100 instead of 1.00).

There is however a solution.

If you check the comments on the following page: http://php.net/manual/en/function.floatval.php

You can see that someone submitted a tofloat function which will solve the issue using the last comma or dot to perform the conversion.

Enjoy.

Sign up to request clarification or add additional context in comments.

1 Comment

Mind that the function assumes you always have a decimal symbol.
1
$netRate  = number_format( $_POST['netrate'] ,2,".","");
$payloads = number_format( $_POST['topay'] ,2,".","");

1 Comment

unfortunately when I do this, I get blank as the output for $netRate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.