PHP Adding 2 decimal points numbers (money) gives wrong results in total amount
<p>I have an customer invoice table in my MySQL database with a DECIMAL(10,2) field called price.</p>

<p>When fetching these values in php and calculating a sum amount, </p>

<p>ex: in the script </p>

<pre><code>$totalAmount = 0; // initialised them to 

   while(records){              

       $amount = $inv_amount - ($pay_amount + $onamount); //float i guess. 2.22, 14.22
       $totalAmount = $totalAmount + $amount; //float i guess. 2.22, 14.22 ..etc

    }
</code></pre>

<p>when <code>echo $totalAmount;</code> it has a slight error in the final amount 0.01 however when dealing which large datasets around 20,000 this error becomes very considerable such as 200+</p>

<p>what is the safest way to do this when dealing with prices and such with these numbers in PHP? Or will I end up with potential rounding errors and things like that which are common when working with floating point data types?</p>

<p>is using </p>

<pre><code>round 
number_format
</code></pre>

<p>is the most suitable solution for this type of a financial application ?</p>
